关于几何关系的代码说明

来源:互联网 发布:北京mba学校排名知乎 编辑:程序博客网 时间:2024/04/29 20:02

 关于几何关系的代码说明

很多朋友需要这方面的资料,尤其是代码。我在陆续挖掘和整理的过程中,推翻了自己的开源的设想,形成了一个库——cgal32.dll(win32版,标准C库)。

一、这是基于几个原因:

1、库中个别地方代码采用了非商业开源的代码。我没有权利去公开它们。

2、只有不开源,我写的库大家才能免费使用。免除大家在道德和法律上面临的风险。

3、我做了大量工作,使自己本身免除道德和法律上的风险。

4、我声明我个人拥有这些库(编译后)的版权。今后任何个人和公司可以免费使用库,但不能声明拥有版权(或其他权利,包括出售库本身的权利)。我拥有库的源代码90%以上的版权。

二、关于cgal32.dll:

1、cgal32.dll与CGAL开源项目(www.cgal.org)没有任何关系,我也没看过和下载过它们的代码。

2、我使用ANSI C构建cgal32.dll库,以使其适应跨平台的需要。虽然目前只有WIN32版本,但以后会出LINUX等版本。

3、cgal32.dll主要完成空间几何的定义和运算。空间几何包括:点(点集)、线(多线)、面、体和面片。运算包括:Union、Difference、XOR、Intersection。

4、我的目标是把cgal32发展成为具有巨大潜力的解决人们在空间几何领域的诸多问题。

5、目前功能简单,还不提供下载,请谅解。

三、附部分头文件:

 

/*=============================================================================
cgal.h    
cheungmine - All rights reserved
    Sep. 8, 2010
==============================================================================
*/

#ifndef _CGAL_H_INCLUDED
#define _CGAL_H_INCLUDED

#include 
<stdio.h>
#include 
<float.h>
#include 
<assert.h>
#include 
<memory.h>

#ifdef __cplusplus
extern "C" {
#endif


#ifdef CGAL_DLLEXPORT
#  define CGAL_CALL __declspec(dllexport)
#  define CGAL_CALL1(x)  __declspec(dllexport) x
#endif

#ifndef CGAL_CALL
#  define CGAL_CALL
#endif

#ifndef CGAL_CALL1
#  define CGAL_CALL1(x)      x CGAL_CALL    
/* For example: CGAL_CALL1(char*) foo();*/
#endif

#include 
"cgalerr.h"
#include 
"cgaldef.h"


/*==============================================================================
                            Library Functions - cgal.c
==============================================================================
*/


void   CGAL_CALL  CGAL_init  ( double epsilon );    /* must call this first of all in process-wide */
void   CGAL_CALL  CGAL_fini  ( int reserved );

BOOL   CGAL_CALL  CGAL_blob_alloc  ( cg_blob_t
* blob );
void   CGAL_CALL  CGAL_blob_free  ( cg_blob_t* blob );

double CGAL_CALL  CGAL_get_epsilon();        /* Smallest such that x+epsilon != x */
int    CGAL_CALL  CGAL_get_precision();        /* decimal digits of precision */

CGAL_CALL1(
const char*)  CGAL_get_result_msg (int rcode);

RESULT CGAL_CALL  CGAL_gzip_deflate ( cg_blob_t
* in_plain, cg_blob_t* out_compressed );            /* compress */

RESULT CGAL_CALL  CGAL_gzip_inflate ( cg_blob_t
* in_compressed, cg_blob_t* out_decompressed );    /* uncompress */

/*==============================================================================
                            Polygon Functions - polygon.c
==============================================================================
*/


BOOL   CGAL_CALL  CG_polygon_create  ( cg_polygon  
*polygon );
void   CGAL_CALL  CG_polygon_destroy  ( cg_polygon polygon );
void   CGAL_CALL  CG_polygon_clear  ( cg_polygon polygon );

void   CGAL_CALL  CG_polygon_read  ( FILE *infile, cg_polygon polygon );
void   CGAL_CALL  CG_polygon_write  ( FILE *outfile, const cg_polygon polygon );
void   CGAL_CALL  CG_polygon_add_part  ( cg_polygon polygon, const cg_vertexlist_t *part, int is_hole );

BOOL   CGAL_CALL  CG_polygon_is_null  ( 
const cg_polygon polygon );
int    CGAL_CALL  CG_polygon_get_parts  ( const cg_polygon polygon, int* part_holes /*callee alloc memory, NULL for retrieving parts count.*/);
int    CGAL_CALL  CG_polygon_get_vertices  ( const cg_polygon polygon, int part_index, cg_vertex_t* vertices /*callee alloc memory, NULL for retrieving vertices count of part by index*/);
BOOL   CGAL_CALL  CG_polygon_part_is_hole  ( 
const cg_polygon polygon, int part_index);
double CGAL_CALL  CG_polygon_get_area  ( const cg_polygon polygon );
double CGAL_CALL  CG_polygon_get_length  ( const cg_polygon polygon );
void   CGAL_CALL  CG_polygon_get_extent  ( const cg_polygon polygon, cg_rect_t* extent );
BOOL   CGAL_CALL  CG_polygon_get_relation  ( CG_RELATION rel, cg_polygon primary, cg_polygon secondary );

void   CGAL_CALL  CG_polygon_clip  ( CG_OPCLIP opclip, cg_polygon subject, cg_polygon clip, cg_polygon result );
void   CGAL_CALL  CG_polygon_makeup  ( cg_polygon  polygon );
void   CGAL_CALL  CG_polygon_copy  ( const cg_polygon inpl, cg_polygon outpl );
BOOL   CGAL_CALL  CG_polygon_pt_inside  ( 
const cg_polygon polygon, const cg_vertex_t* pt );
void   CGAL_CALL  CG_polygon_to_tristrip  ( cg_polygon polygon, cg_tristrip tristrip );
void   CGAL_CALL  CG_rect_to_polygon  ( const cg_rect_t* rc, cg_polygon outpl );
void   CGAL_CALL  CG_circle_to_polygon (double center_xp, double center_yp, double radius, int max_circle_points, cg_polygon outpl);

void   CGAL_CALL  CG_line_make_buffer  ( const cg_vertex_t* start, const cg_vertex_t* end, double distance, int mcp, cg_polygon out_pl );
void   CGAL_CALL  CG_vertices_make_buffer  ( const cg_vertex_t* vertices, int num_vertices, double distance, int mcp, cg_polygon outpl );
void   CGAL_CALL  CG_polyline_make_buffer  ( const cg_vertexlist_t* vl, double distance, int mcp, int closed, cg_polygon out_pl );
void   CGAL_CALL  CG_polygon_make_buffer  ( cg_polygon inpl, double distance, int mcp, cg_polygon outpl );


/*==============================================================================
                            Trilist/Trinet/Trifan/Tristrip Functions - tri.c
==============================================================================
*/


BOOL   CGAL_CALL  CG_tristrip_create  ( cg_tristrip 
*tristrip );
void   CGAL_CALL  CG_tristrip_free  ( cg_tristrip tristrip );

int    CGAL_CALL  CG_tristrip_get_strips ( const cg_tristrip tristrip );
int    CGAL_CALL  CG_tristrip_get_vertices ( const cg_tristrip tristrip, int strip_index, cg_vertex_t* vertices );
void   CGAL_CALL  CG_tristrip_clip  ( CG_OPCLIP opclip, cg_polygon subject, cg_polygon clip, cg_tristrip result );




/*==============================================================================
                            Vertex and Line Functions - cgal.c
==============================================================================
*/


double CGAL_CALL  CG_vertex_get_dist  ( const cg_vertex_t* p, const cg_vertex_t* q );
double CGAL_CALL  CG_vertex_get_dist_sq  ( const cg_vertex_t* p, const cg_vertex_t* q );
double CGAL_CALL  CG_vertex_get_area  ( const cg_vertex_t* vl, int num_vertices );
double CGAL_CALL  CG_vertex_get_length  ( const cg_vertex_t* vl, int num_vertices, int closed );
void   CGAL_CALL  CG_vertex_get_extent  ( const cg_vertex_t* vl, int num_vertices, cg_rect_t* rc );
BOOL   CGAL_CALL  CG_vertex_is_equal  ( 
const cg_vertex_t* p, const cg_vertex_t* q );

void   CGAL_CALL  CG_vertex_offset  ( cg_vertex_t* vertices, int num_vertices, double dx, double dy );

BOOL   CGAL_CALL  CG_line_offset  ( cg_vertex_t
* start, cg_vertex_t* end, double d);  /* d >0 to the left, d<0 to the right */
BOOL   CGAL_CALL  CG_vertices_pt_inside  ( 
const cg_vertex_t* vertices, int num_vertices, const cg_vertex_t* pt );


/*==============================================================================
                            Rect Functions - cgal.c
==============================================================================
*/


BOOL   CGAL_CALL  CG_rect_pt_inside  ( 
const cg_rect_t *rect, const cg_vertex_t* pt );
BOOL   CGAL_CALL  CG_rect_is_overlapped  ( 
const cg_rect_t* rc1, const cg_rect_t* rc2 );
BOOL   CGAL_CALL  CG_rect_clip_line  ( 
const cg_rect_t* rect, cg_vertex_t* p, cg_vertex_t* q );


/*==============================================================================
                            Shape Functions - shape.c
==============================================================================
*/


BOOL   CGAL_CALL  CG_shape_create  ( cg_shape
* shape );
void   CGAL_CALL  CG_shape_destroy  ( cg_shape shape );
void   CGAL_CALL  CG_shape_clear  ( cg_shape shape );

void   CGAL_CALL  CG_shape_read  ( FILE *infile, cg_shape shape );
void   CGAL_CALL  CG_shape_write  ( FILE *outfile, const cg_shape shape );

size_t CGAL_CALL  CG_shape_get_byte_size  ( 
const cg_shape shape );
RESULT CGAL_CALL  CG_shape_read_blob  (  
const cg_blob_t*  blob, cg_shape shape );
size_t CGAL_CALL  CG_shape_write_blob  ( cg_blob_t
* blob, const cg_shape shape );

void   CGAL_CALL  CG_shape_copy  ( const cg_shape in_shape, cg_shape out_shape );

BOOL   CGAL_CALL  CG_shape_is_null  ( 
const cg_shape shape );
int    CGAL_CALL  CG_shape_get_type  ( const cg_shape shape );
int    CGAL_CALL  CG_shape_get_parts ( const cg_shape shape, int* part_offsets );
int    CGAL_CALL  CG_shape_get_subparts ( const cg_shape shape, int part_index, int* subpart_offsets );
int    CGAL_CALL  CG_shape_get_vertices ( const cg_shape shape, int part_index, int subpart_index, cg_vertex_t* vertices );
int    CGAL_CALL  CG_shape_get_all_subparts ( const cg_shape shape, int* subpart_offsets );
int    CGAL_CALL  CG_shape_get_all_vertices ( const cg_shape shape, cg_vertex_t* vertices );


void   CGAL_CALL  CG_polygon_to_shape (cg_polygon pl, cg_shape sp);

void   CGAL_CALL  CG_shapeto_polygon (const cg_shape sp, cg_polygon pl);


/*==============================================================================
                            Object Functions - object.c
==============================================================================
*/







/*==============================================================================
                            Gemoetry Functions - geometry.c
==============================================================================
*/

BOOL   CGAL_CALL    CG_geometry_create  (cg_geometry
* g);

void   CGAL_CALL    CG_geometry_destroy  (cg_geometry g);
void   CGAL_CALL    CG_geometry_clear  (cg_geometry g);

BOOL   CGAL_CALL    CG_geometry_is_null  (
const cg_geometry g);

size_t CGAL_CALL    CG_geometry_get_byte_size  (
const cg_geometry g);
size_t CGAL_CALL    CG_geometry_write_blob  (cg_blob_t
* blob, const cg_geometry g);
RESULT CGAL_CALL    CG_geometry_read_blob  (
const cg_blob_t* blob, const cg_geometry g);

RESULT CGAL_CALL    CG_shape_to_geometry  (
const cg_shape sp, cg_geometry g);





/*=============================================================================*/
#ifdef __cplusplus
}

#endif

#endif /* ndef _CGAL_H_INCLUDED */

 

/*=============================================================================
    cgaldef.h
    cheungmine - All rights reserved
    Sep. 8, 2007
=============================================================================
*/
#ifndef _CGALDEF_H_INCLUDED
#define _CGALDEF_H_INCLUDED

#ifndef CG_PI
#define CG_PI            3.1415926535897932384626433832795
#endif

#ifndef CG_HFPI
#define CG_HFPI         1.5707963267948966192313216916398
#endif
    
#ifndef CG_BIPI
#define CG_BIPI         6.283185307179586476925286766559
#endif

#ifndef TRUE
#define FALSE           0
#define TRUE            1
#endif

typedef 
int                BOOL;
typedef unsigned 
char    BYTE;
typedef unsigned 
int    size_t;


/*=============================================================================
                    Opaque Struct Handles
=============================================================================
*/
typedef  
struct  _cg_polygon_t*        cg_polygon;

typedef  
struct  _cg_trilist_t*        cg_trilist;
typedef  
struct  _cg_trinet_t*        cg_trinet;
typedef  
struct  _cg_trifan_t*        cg_trifan;
typedef  
struct  _cg_tristrip_t*    cg_tristrip;

typedef  
struct  _cg_trilistZ_t*    cg_trilistZ;
typedef  
struct  _cg_trinetZ_t*        cg_trinetZ;
typedef  
struct  _cg_trifanZ_t*        cg_trifanZ;
typedef  
struct  _cg_tristripZ_t*    cg_tristripZ;

typedef  
struct  _cg_shape_t*        cg_shape;

typedef  
struct  _cg_arclist_t*        cg_arclist;
typedef  
struct  _cg_arclistZ_t*    cg_arclistZ;

typedef  
struct  _cg_object_t*        cg_object;

typedef  
struct  _cg_patch_t*        cg_patch;
typedef  
struct  _cg_patchZ_t*        cg_patchZ;

typedef  
struct  _cg_geometry_t*    cg_geometry;


/*=============================================================================
                        Public  Types
=============================================================================
*/
/* shape type */
typedef 
enum   
{
    SHP_NULL    
= 0,        /* Null  type*/
    SHP_POINT   
= 1,        /* Point type*/
    SHP_LINE    
= 2,        /* Line type*/
    SHP_POLYGON 
= 3            /* Polygon type*/
} CG_SHAPETYPE;

/* arc type */
typedef 
enum   
{
    ARC_LINE        
=1,
    ARC_CIRCLE_P    
=2,
    ARC_CIRCLE_C    
=3,
    ARC_ELLIPSE        
=4,
    ARC_SPLINE        
=10,    
    ARC_BEZIER        
=20
} CG_ARCTYPE;

/* 3D Object Types - standard object primitives */
typedef 
enum  
{
    OBJ_BOX                
= 1,
    OBJ_CONE            
= 2,
    OBJ_SPERE            
= 3,
    OBJ_HEMISPHERE        
= 4,
    OBJ_GEOSPHERE        
= 5,
    OBJ_CYLINDER        
= 6,
    OBJ_TUBE            
= 7,
    OBJ_TORUS            
= 8,
    OBJ_PYRMID            
= 9,
    OBJ_PLANE            
= 10    
} CG_OBJECTTYPE;

/* extended object primitives */
typedef 
enum  
{
    OBJEX_ELLIPSOID        
= 16,
    OBJEX_OVAL            
= 17,
    OBJEX_HEDRA            
= 18,
    OBJEX_CHAMFERBOX    
= 19,
    OBJEX_CHAMFERCYL    
= 20,
    OBJEX_SPINDLE        
= 21,
    OBJEX_GENGON        
= 22,
    OBJEX_PRISM            
= 23,
    OBJEX_HOSE            
= 24,
    OBJEX_L_EXT            
= 25,
    OBJEX_C_EXT            
= 26,
    OBJEX_CAPSULE        
= 27,
    OBJEX_TORUSKNOT        
= 28,
    OBJEX_OILTANK        
= 29,
    OBJEX_RINGWAVE        
= 30,
    OBJ_TEAPOT            
= 31
} CG_OBJECTTYPE_EX;

/* Patch type */
typedef 
enum   
{
    PAT_TRILIST            
= 1,
    PAT_TRINET            
= 2,    
    PAT_TRIFAN            
= 3,
    PAT_TRISTRIP        
= 4
} CG_PATCHTYPE;

/* 3D Patch type */
typedef 
enum   
{    
    PAT_TRILISTZ        
= 5,
    PAT_TRINETZ            
= 6,
    PAT_TRIFANZ            
= 7,
    PAT_TRISTRIPZ        
= 8
} CG_PATCHZTYPE;

/* Geometry type */
typedef 
enum  
{
    GT_NULL                
= 0,    
    GT_SHAPE            
= 1,    
    GT_VERTEX            
= 2,
    GT_VERTEXZ            
= 3,    
    GT_POLYLINE            
= 4,
    GT_POLYLINEZ        
= 5,
    GT_POLYGON            
= 6,        
    GT_ARCLIST            
= 7,    /* Compound arc line */
    GT_ARCLISTZ            
= 8,    /* Compound arc with z */
    GT_PATCH            
= 9,    
    GT_PATCHZ            
= 10,
    GT_OBJECT            
= 11    /* Object */    
} CG_GEOMETRYTYPE;


/*=============================================================================
                        Enum Constants
=============================================================================
*/
typedef 
enum                         /* Set operation type                */
{
    CG_DIFF,                         
/* Difference                        */
    CG_INT,                          
/* Intersection                      */
    CG_XOR,                          
/* Exclusive or                      */
    CG_UNION                         
/* Union                             */
} CG_OPCLIP;

typedef 
enum   /* relationships */
{
    REL_CONTAIN     
= 0,        /* TRUE if the primary shape wholly contains the secondary shape */
    REL_CROSS     
= 1,        /* TRUE if the intersection of the interiors is a lower dimension than the maximum dimension of the two shapes */
    REL_DISJOINT 
= 2,        /* TRUE if the intersection of two shapes is empty */
    REL_EQUAL    
= 3,        /* TRUE if the two supplied shapes are identical */
    REL_OVERLAP  
= 4,        /* TRUE if the intersection of two shapes results in an object of the same geometric dimension */
    REL_TOUCH    
= 5,        /* TRUE if the two supplied shapes share a common boundary */
    REL_WITHIN   
= 6        /* TRUE if primary_shape is wholly contained within secondary_shape */
} CG_RELATION;


/*=============================================================================
                    Public  Struct Types
=============================================================================
*/
/* Vertex structure */
typedef 
struct                       
{
    union{
        
struct{    double  x, y;    };
        
double            _v[2];
    };
} cg_vertex_t, cg_point_t;

/* Line structure */
typedef 
struct      
{
    union{
        
structdouble  x1, y1,  x2,  y2; };
        
struct{ cg_point_t    start, end;   };
        cg_point_t    _p[
2];
        
double        _v[4];
    };    
} cg_edge_t, cg_line_t;

/* Vertex list structure */
typedef 
struct                       
{
    
int                num_vertices;        /* Number of vertices in list */
    cg_vertex_t        
*vertex;            /* Vertex array pointer */
} cg_vertexlist_t, cg_pointlist_t, cg_polyline_t;

/* rectangle type*/
typedef 
struct
{
    union{
        
struct{
            union{    
                
struct{    double  min_x, min_y; };
                cg_vertex_t        _min;
            };
            union{
                
struct{    double  max_x, max_y; };
                cg_vertex_t        _max;
            };
        };
        
double   _v[4];
    };
} cg_rect_t;

/* 3D rect - box type*/
typedef 
struct
{
    union{
        
struct{
            union{
                
struct{    double  min_x, min_y, max_x, max_y;    };
                cg_rect_t        _rc;
            };    
            
double      min_z,    max_z;
        };
        
double            _v[6];
    };
} cg_rectZ_t, cg_box_t;

/* circleCR struct */
typedef 
struct
{
    union{
        
struct{    double  xp,  yp; };
        cg_vertex_t        _cp;
    };
    
double     r;
} cg_circle_t;

/* Point structure */
typedef 
struct                      
{
    union{
        
struct{
            union{
                
struct{    double  x, y; };
                cg_vertex_t        _p;
            };
            
double    z;
        };
        
double  _v[3];
    };
} cg_vector_t, cg_pointZ_t, cg_vertexZ_t;

/* 3D Vertex list structure */
typedef 
struct                         
{
    
int                 num_vertices;  /* Number of vertices */
    cg_vector_t         
*vertex;       /* Vertex array */
} cg_vectorlist_t, cg_pointlistZ_t, cg_vertexlistZ_t;

/* Triangle struct */
typedef 
struct
{
    union{
        
struct{    cg_vertex_t    v0, v1, v2; };
        cg_vertex_t            _v[
3];
    };
} cg_tri_t;

/* 3D Triangle struct */
typedef 
struct
{
    union{
        
struct{    cg_vector_t v0, v1, v2;    };
        cg_vector_t            _v[
3];
    };
    cg_vector_t           n;            
/* Normal vector */
} cg_triZ_t;

/* triangle vertex index struct */
typedef 
struct
{
    union{
        
struct{    int i0, i1, i2;    };
        
int            _vi[3];
    };    
} cg_triindex_t;

/* BLOB struct */
typedef 
struct
{
    BYTE
*          chunk;
    size_t        size;
} cg_blob_t;


/*============================================================================*/
#endif /* ndef _CGALDEF_H_INCLUDED */