Esempio n. 1
0
polygon_t* PolygonFromJPoly( void )
{
	polygon_t	*p;
	int		start;
	int		i;


	if ( jtype == -1 )
	{
		start = 0;
	}
	else if ( jtype == 1024 )
	{
		start = 0;
		InsertPointAt( 0, jcenter );	
		// the first point of the polygon
		// is its center
	}
	else 
	{
		start = jtype;
	}

	p = NewPolygon( jpnum );
	p->pointnum = jpnum;

	for ( i = 0; i < jpnum; start++, i++ )
	{
		start = (start == jpnum ) ? 0 : start;
		Vec3dCopy( p->p[i], jpoly[start] );
	}
	
	return p;
}
Esempio n. 2
0
/*
  ==============================
  CreatePolygonFromClass

  ==============================
*/
polygon_t * CreatePolygonFromClass( hobj_t *polygon )
{
	int		pointnum;
	hpair_t		*pair;
	polygon_t	*p;
	int		i;
	char		tt[256];
	
	if ( strcmp( polygon->type, "polygon" ) )
		Error( "class [%s,%s] is not of type 'polygon'\n", polygon->type, polygon->name );

	pair = FindHPair( polygon, "pointnum" );
	if ( !pair )
		pair = FindHPair( polygon, "num" );
	if ( !pair )
		Error( "can't get pointnum of [%s,%s]\n", polygon->type, polygon->name );

	HPairCastToInt( &pointnum, pair );

	p = NewPolygon( pointnum );
	p->pointnum = pointnum;

	for ( i = 0; i < pointnum; i++ )
	{
		sprintf( tt, "%d", i );
		pair = FindHPair( polygon, tt );
		if ( !pair )
			Error( "can't get point '%s' of [%s,%s]\n", tt, polygon->type, polygon->name );

		HPairCastToVec3d( p->p[i], pair );
	}

	return p;
}
Esempio n. 3
0
/*
  ==================================================
  read classes

  ==================================================
*/
face_t * NewFaceFromPolygonClass( hobj_t *poly )
{
	polygon_t	*p, *p2;
	hpair_t		*pair;
	int		pointnum;
	int		i;
	face_t		*f;
	char		tt[256];

	f = (face_t *) malloc( sizeof( face_t ) );
	memset( f, 0, sizeof( face_t ) );

	Vec3dInitBB( f->min, f->max, 999999.9 );
	
	pair = FindHPair( poly, "num" );
	if ( !pair )
		Error( "missing polygon pointnum.\n" );
	HPairCastToInt_safe( &pointnum, pair );
	p = NewPolygon( pointnum );
	p->pointnum = pointnum;
	for ( i = 0; i < pointnum; i++ )
	{
		sprintf( tt, "%d", i );
		pair = FindHPair( poly, tt );
		if ( !pair )
			Error( "missing point.\n" );
		HPairCastToVec3d_safe( p->p[i], pair );
		Vec3dAddToBB( f->min, f->max, p->p[i] );
		Vec3dAddToBB( p_min, p_max, p->p[i] );
	}	

	p2 = PolygonRemoveEqualPoints( p );
	if ( p->pointnum != p2->pointnum )
	{
//		printf( "removed equal points !\n" );
		FreePolygon( p );
		f->p = p2;
		f->fix = p2;	// hack
	}
	else
	{
		FreePolygon( p2 );
		f->p = p;
		f->fix = p;	// hack
	}

	// calc plane
	PlaneFromPolygon( f->p, f->norm, &f->dist );
	
	// expand bb by equal_dist
	for ( i = 0; i < 3; i++ )
	{
		f->min[i] -= equal_dist;
		f->max[i] += equal_dist;
	}

	return f;
}
Polygon CopyPolygon( Polygon poly ) {
	if ( !PolygonIsGood(poly) ) return NULL;
	Polygon copy = NewPolygon(poly->numberOfVertices);
	if ( !PolygonIsGood(copy) ) return NULL;
	memcpy(copy->vertices,poly->vertices,sizeof(struct PointSt)*(poly->numberOfVertices));
	copy->numberOfVertices = poly->numberOfVertices;
	copy->cursor = poly->cursor;
	return copy;
}
Esempio n. 5
0
void PolygonsFromJPoly( polygon_t **p1, polygon_t **p2 )
{
	int		i, num;

	if ( jtype != 1024 )
		Error( "only one polygon.\n" );
	
	// count points of p1
	for ( i = splitvertex1, num = 0; i != splitvertex2; (i = ((i+1)==jpnum) ? 0 : (i+1)), num++ )
	{ }
	num++;

	*p1 = NewPolygon( num );
	(*p1)->pointnum = num;
	// build p1
	for ( i = splitvertex1, num = 0; ; i = ((i+1)==jpnum) ? 0 : (i+1), num++ )
	{ 
		Vec3dCopy( (*p1)->p[num], jpoly[i] );

		if ( i == splitvertex2 )
			break;
	}
		
	// count points of p2
	
	for ( i = splitvertex2, num = 0; i != splitvertex1; (i = ((i+1)==jpnum) ? 0 : (i+1)), num++ )
	{ }
	num++;

	*p2 = NewPolygon( num );
	(*p2)->pointnum = num;
	// build p2
	for ( i = splitvertex2, num = 0; ; i = ((i+1)==jpnum) ? 0 : (i+1), num++ )
	{ 
		Vec3dCopy( (*p2)->p[num], jpoly[i] );

		if ( i == splitvertex1 )
			break;
	}
}
Esempio n. 6
0
//
// PolygonFlip
//
polygon_t*	PolygonFlip( polygon_t *in )
{
	int		i;
	polygon_t	*flip;

	flip = NewPolygon( in->pointnum );
	flip->pointnum = in->pointnum;

	for ( i = 0; i < in->pointnum; i++ )
		Vec3dCopy( flip->p[(in->pointnum-1)-i], in->p[i] );

	return flip;
}
Esempio n. 7
0
polygon_t*  BasePolygonForPlane( vec3d_t norm, fp_t dist )
{
	int		i;
	vec3d_t		absnorm;
	vec3d_t		org, vup, vright;
	fp_t		d;
	polygon_t	*p;

	
	Vec3dInit( vup, 0,0,0 );
	p = NewPolygon( 4 );
	
	for ( i=0; i<3; i++ )
		absnorm[i] = fabs( norm[i] );

	if ( absnorm[0] >= absnorm[1] && absnorm[0] >= absnorm[2] )
                vup[2] = 1.0;
        else if ( absnorm[1] >= absnorm[0] && absnorm[1] >= absnorm[2] )
                vup[2] = 1.0;
        else if ( absnorm[2] >= absnorm[0] && absnorm[2] >= absnorm[1] )
                vup[0] = 1.0;
	
	d = Vec3dDotProduct( vup, norm );
	Vec3dMA( vup, -d, norm, vup );
	Vec3dUnify( vup );
	
	Vec3dScale( org, dist, norm );
	
	Vec3dCrossProduct( vright, norm, vup );
	Vec3dScale( vup, BASE_POLYGON_SIZE, vup );
	Vec3dScale( vright, BASE_POLYGON_SIZE, vright );


	Vec3dSub( p->p[0], org, vright );
	Vec3dSub( p->p[0], p->p[0], vup );
	
	Vec3dSub( p->p[1], org, vright );
	Vec3dAdd( p->p[1], p->p[1], vup );
	
	Vec3dAdd( p->p[2], org, vright );
	Vec3dAdd( p->p[2], p->p[2], vup );
	
	Vec3dAdd( p->p[3], org, vright );
	Vec3dSub( p->p[3], p->p[3], vup );   	

	p->pointnum = 4;
	return p;	
}
Esempio n. 8
0
polygon_t * PolygonRemoveEqualPoints( polygon_t *in )
{
	polygon_t		*p;
	int			i;

	p = NewPolygon( in->pointnum );
	for ( i = 0; i < in->pointnum; i++ )
	{
		if ( CheckSame( in->p[i], in->p[(i+1)%in->pointnum] ) )
		{
			continue;
		}
		Vec3dCopy( p->p[p->pointnum], in->p[i] );
		p->pointnum++;
	}
	return p;
}
Polygon LinePolygonIntersection( Polygon poly, float x1, float y1, float x2, float y2 ) {
	int size = poly->numberOfVertices, i, k;
	float x3, y3, x4, y4, xInt, yInt;
	Polygon intersections = NewPolygon(10);
	for (i=0;i<size;i++) {
		x3 = poly->vertices[i].x;
		y3 = poly->vertices[i].y;
		k = ( i + 1 ) % size;
		x4 = poly->vertices[k].x;
		y4 = poly->vertices[k].y;
		k = LineLineIntersection( x1, y1, x2, y2, x3, y3, x4, y4, &xInt, &yInt );
		switch ( k ) {
			case 2: AddPolygonVertex(intersections,x3,y3);
					AddPolygonVertex(intersections,x4,y4);
					break;
			case 1: AddPolygonVertex(intersections,xInt,yInt);
			case 0: break;
		}
	}
	
	return intersections;
	
}
Esempio n. 10
0
hobj_t * BuildMeshTileShape( hobj_t *surf_poly, hobj_t *plane, hobj_t *texdef, hpair_t *mat_pair, u_list_t *poly_list )
{
	hobj_t		*shape;
	hobj_t		*tmp;
	hobj_t		*polygon;
	hobj_t		*meshtile;

	u_list_iter_t	iter;
	polygon_t	*p;	

	int		poly_num;

	shape = EasyNewClass( "shape" );

	InsertHPair( shape, NewHPair2( "string", "tess_name", "meshtile" ) );

	polygon = DeepCopyClass( surf_poly );
	EasyNewClsref( polygon, "plane", plane );

	InsertClass( shape, polygon );

	if ( mat_pair )
	{
		InsertHPair( shape, NewHPair2( "ref", "material", mat_pair->value ) );
	}
	else
	{
		InsertHPair( shape, NewHPair2( "ref", "material", "default" ) );
	}

	{
		hobj_t		*texdef0;

		texdef0 = EasyNewClass( "proj_texdef0" );
		InsertClass( shape, texdef0 );
		
		EasyNewClsref( texdef0, "texdef", texdef );
	}


	meshtile = EasyNewClass( "meshtile" );
	InsertClass( shape, meshtile );

#if 0
	poly_num = 0;
	U_ListIterInit( &iter, poly_list );
	for ( ; ( p = U_ListIterNext( &iter ) ) ; )
	{
		if ( p->pointnum == 3 )
		{
			InsertClass( meshtile, CreateClassFromPolygon( p ) );
			poly_num++;
		}
		else
		{
			int		j;
			polygon_t		*tmp;
			
			tmp = NewPolygon( 3 );
			tmp->pointnum = 3;
			
			Vec3dCopy( tmp->p[0], p->p[0] );

			for ( j = 1; j < p->pointnum-1; j++ )
			{

				Vec3dCopy( tmp->p[1], p->p[j] );
				Vec3dCopy( tmp->p[2], p->p[j+1] );
				InsertClass( meshtile, CreateClassFromPolygon( tmp ) );
				poly_num++;
			}
			FreePolygon( tmp );
		}
	}

	EasyNewInt( meshtile, "polynum", poly_num );
#endif

	return shape;
}
Esempio n. 11
0
void BuildTriMesh( hobj_t *shape, u_list_t *poly_list )
{
	u_list_iter_t		iter;
	polygon_t		*p;
	trimesh_t		trimesh;
	unique_t	ids[3];

	TriMesh_Init( &trimesh );

	U_ListIterInit( &iter, poly_list );
	for ( ; ( p = U_ListIterNext( &iter ) ) ; )
	{
		if ( p->pointnum == 3 )
		{
			ids[0] = TriMesh_AddFreeVertex( &trimesh, p->p[0], 0.5 ); 
			ids[1] = TriMesh_AddFreeVertex( &trimesh, p->p[1], 0.5 ); 
			ids[2] = TriMesh_AddFreeVertex( &trimesh, p->p[2], 0.5 ); 

			if ( ids[0] == ids[1] || ids[0] == ids[2] || ids[1] == ids[2] )
			{
			}
			else
			{
				TriMesh_AddTri( &trimesh, UNIQUE_INVALIDE, ids );
			}
			
		}
		else
		{
			int		j;
			polygon_t		*tmp;
			
			tmp = NewPolygon( 3 );
			tmp->pointnum = 3;
			
			Vec3dCopy( tmp->p[0], p->p[0] );

			for ( j = 1; j < p->pointnum-1; j++ )
			{

				Vec3dCopy( tmp->p[1], p->p[j] );
				Vec3dCopy( tmp->p[2], p->p[j+1] );

				ids[0] = TriMesh_AddFreeVertex( &trimesh, tmp->p[0], 0.5 ); 
				ids[1] = TriMesh_AddFreeVertex( &trimesh, tmp->p[1], 0.5 ); 
				ids[2] = TriMesh_AddFreeVertex( &trimesh, tmp->p[2], 0.5 ); 

				if ( ids[0] == ids[1] || ids[0] == ids[2] || ids[1] == ids[2] )
				{
				}
				else
				{
					TriMesh_AddTri( &trimesh, UNIQUE_INVALIDE, ids );
				}

			}
			FreePolygon( tmp );
		}
	}

	TriMesh_Dump( &trimesh );

	{
		hobj_t		*glmesh;		

		printf( "glmesh: ofs %d - ", glmesh_ofs );
		glmesh = TriMesh_BuildGLMesh( &trimesh, glmesh_base, &glmesh_ofs );
		printf( "%d\n", glmesh_ofs-1 );

		InsertClass( shape, glmesh );
	}
}
Esempio n. 12
0
int main( int argc, char *argv[] )
{
	char		*in_brush_name;
	char		*in_plane_name;
	char		*out_field_name;

	hmanager_t	*planehm;
	hmanager_t	*brushhm;

	map3_t		*map;
	FILE		*h;

	printf( "===== field2 - build field clusters =====\n" );
	SetCmdArgs( argc, argv );

	in_brush_name = GetCmdOpt2( "-b" );
	in_plane_name = GetCmdOpt2( "-pl" );
	out_field_name = GetCmdOpt2( "-o" );

	if ( !in_brush_name )
	{
		in_brush_name = "_gather1_bspbrush.hobj";
		printf( " default input brush class: %s\n", in_brush_name );
	}
	else
	{
		printf( " input brush class: %s\n", in_brush_name );
	}

	if ( !in_plane_name )
	{
		in_plane_name = "_plane.hobj";
		printf( " default input plane class: %s\n", in_plane_name );
	}
	else
	{
		printf( " input plane class: %s\n", in_plane_name );
	}

	if ( !out_field_name )
	{
		out_field_name = "_fieldmap.bin";
		printf( " default output field binary: %s\n", out_field_name );
	}
	else
	{
		printf( " output field binary: %s\n", out_field_name );
	}

	printf( "loading plane class ...\n" );
	planehm = ReadPlaneClass( in_plane_name );	

	printf( "loading brush class ...\n" );
	if ( !(brushhm = NewHManagerLoadClass( in_brush_name ) ) )
		Error( "failed\n" );

	map = NewMap3Hash();

	{
		hobj_search_iterator_t	brushiter;
		hobj_search_iterator_t	surfiter;
		hobj_search_iterator_t	polyiter;
		
		hobj_t		*brush;
		hobj_t		*surface;
		hobj_t		*plane;
		hobj_t		*texdef;
		hobj_t		*poly;
		hpair_t		*pair;

		cplane_t	*pl;
	
		polygon_t	*p;
		int		i, num;		
		char		tt[256];
	
		InitClassSearchIterator( &brushiter, HManagerGetRootClass( brushhm ), "bspbrush" );
		for ( ; ( brush = SearchGetNextClass( &brushiter ) ); )
		{
			InitClassSearchIterator( &surfiter, brush, "surface" );
			for ( ; ( surface = SearchGetNextClass( &surfiter ) ); )
			{
				
				pair = FindHPair( surface, "plane" );
				if ( !pair )
					Error( "missing 'plane' in surface '%s'.\n", surface->name );
				plane = HManagerSearchClassName( planehm, pair->value );
				if ( !plane )
					Error( "surface '%s' can't find plane '%s'.\n", surface->name, pair->value );
				pl = GetClassExtra( plane );
				
				InitClassSearchIterator( &polyiter, surface, "polygon" );
				for ( ; ( poly = SearchGetNextClass( &polyiter ) ); )
				{
					pair = FindHPair( poly, "num" );
					if ( !pair )
						Error( "missing pointnum 'num' of polygon '%s'.\n", poly->name );
					HPairCastToInt_safe( &num, pair );
					
					p = NewPolygon( num );
					p->pointnum = num;
					
					for ( i = 0; i < num; i++ )
					{
						sprintf( tt, "%d", i );
						pair = FindHPair( poly, tt );
						if ( !pair )
							Error( "missing point '%s' of polygon '%s'.\n", tt, poly->name );
						HPairCastToVec3d( p->p[i], pair );
					}

					DistributePolygon( map, p, pl );					
				}
			}
		}
	}	

	Map3Normalize( map );

	printf( " %d field patches\n", fieldpatchnum );
	printf( " %d field cells\n", fieldcellnum );

	h = fopen( out_field_name, "w" );
	if ( !h )
		Error( "can't open output file.\n" );
	WriteMap3( map, h );
	fclose( h );
	
}
Esempio n. 13
0
void FixPolygon( polygon_t **inout, vec3d_t center, vec3d_t norm )
{
	polygon_t	*p;
	int		num, num2;
	int		flags[256];
	int		i, j;
	vec3d_t		v1, v2;
	vec3d_t		tmp;
	fp_t		min, l;
	int		best;

	num = (*inout)->pointnum;
	p = NewPolygon( num );
	p->pointnum = num;

	memset( flags, 255, 256*4 );


	flags[0] = 0;
	Vec3dCopy( p->p[0], (*inout)->p[0] );
	i = 0;
	for ( num2 = 1; num2 < num; )
	{
		min = 999999.9;
		best = -1;
		for ( j = 1; j < num; j++ )
		{
			if ( !flags[j] )
				continue;

//			printf( "%d %d: ", i, j );
			Vec3dSub( v2, (*inout)->p[j], (*inout)->p[i] );
			Vec3dSub( v1, center, (*inout)->p[i] );
			Vec3dCrossProduct( tmp, v1, v2 );
			Vec3dPrint( tmp );
//			getchar();
			Vec3dUnify( tmp );
			Vec3dAdd( tmp, norm, tmp );
//			printf( "best %f\n", Vec3dLen( tmp ) );

			if ( Vec3dLen( tmp ) < 1.9 )	// clockwise ?
				continue;
			Vec3dSub( tmp, (*inout)->p[j], (*inout)->p[i] );
			l = Vec3dLen( tmp );
			if ( l < min )
			{
				min = l;
				best = j;
			}

		}

		if ( best == -1 )
			Error("error\n");

		Vec3dCopy( p->p[num2], (*inout)->p[best] );
		flags[best] = 0;
		i = best;
		num2++;
	}

	FreePolygon( *inout );
	*inout = p;
}
Esempio n. 14
0
// Reads the scene file and stores the information as PolygonGroup and Polygon structures
bool ReadSceneFile(void) 
{
	FILE *SceneFile;				// File pointer for the scene file
	PolygonGroup *Head = NULL;		// Head of the linked list of polygon groups
	int nPolygons = 0;				// Number of polygons read from the scene file
	Coordinate MaxCoordinates;		// The maximum coordinates observed in the scene

	// Attempt to open the scene file
	if (fopen_s(&SceneFile, SceneFilename, "r") != 0) {
		printf("Could not open scene file '%s'\n", SceneFilename);
		return false;
	}
	else {
		// Flags used whilst reading scene file data
		bool EndOfFile;
		bool ReadingParameters;
		bool SuccessfulRead;
		bool FirstGroup;
		
		char *Buffer;				// Text buffer for reading strings from the scene file
		char *Context = NULL;		// Used for getting tokens from the buffer string
		char *Parameter;			// Stores the identifier letter at the start of each scene file string
		char *Comment;				// Used in removing comments from strings
		int LineCount;					// Used for monitoring the position of errors in the scene file
		Coordinate CoordinateBuffer;	// Stores coordinates read from the buffer
		Coordinate Offset;						// Stores the current coordinate offset
		PolygonGroup *PolygonGroupBuffer;		// Stores polygon group parameters as they are read from the file
		Polygon *PolygonBuffer;					// Stores polygon parameters as they are read from the file
		Polygon **PolygonListTail;				// Tail of the linked list of polygons

		// Allocate memory for a new polygon group and the coordinate buffer
		PolygonGroupBuffer = NewPolygonGroup(NULL);
		PolygonListTail = &PolygonGroupBuffer->PolygonList;
		
		// Initiate the buffers
		CoordinateBuffer.X = 0;
		CoordinateBuffer.Y = 0;
		CoordinateBuffer.Z = 0;
		Offset.X = 0;
		Offset.Y = 0;
		Offset.Z = 0;

		// Allocate memory for the buffer
		Buffer = (char*) malloc(200*sizeof(char));

		// Initialise the flags
		EndOfFile = false;
		ReadingParameters = false;
		SuccessfulRead = true;
		FirstGroup = true;

		// Initialise the line count to 0
		LineCount = 0;

		printf("\nReading polygon information from scene file '%s'\n\n", SceneFilename);

		// Read the scene file, one line at a time
		do {
			if (fgets(Buffer, 200, SceneFile) != NULL) {
								
				// Keep track of the line number
				LineCount++;
				// Search for and remove comments				
				Comment = strstr(Buffer, "//");
				if (Comment != NULL) {
					Comment[0] = NULL;	// Replace with 0 to mark a new end of the string
				}

				// Remove leading whitespace and read the parameter type character (p,t,z,o,h,v)
				Parameter = strtok_s(Buffer, "\t ", &Context);
				if (Parameter != NULL) {
					switch (Parameter[0]) {
						
						// Read the electrical parameters
						case 'p':
							// Set the reading parameters flag, create a new polygon group and add the old one to the list
							ReadingParameters = true;
							// Add the polygon buffer to the list if this is not the first read
							if (FirstGroup == true) {
								FirstGroup = false;
							}
							else {
								AddPolygonGroupToList(&Head, PolygonGroupBuffer);
								PolygonGroupBuffer = NewPolygonGroup(PolygonGroupBuffer);						
								PolygonListTail = &PolygonGroupBuffer->PolygonList;
								// FIXME: Should the offset be reset upon reading a 'p'?
								//Offset.X = 0;
								//Offset.Y = 0;
								//Offset.Z = 0;
							}
							SuccessfulRead = ReadParameters(&Context, PolygonGroupBuffer);
							break;
						
						// Read the wall thickness
						case 't':
							// If not currently reading parameters (i.e. not just read a p, z or t) create a new polygon group and add the old one to the list
							if (ReadingParameters == false) {
								ReadingParameters = true;
								AddPolygonGroupToList(&Head, PolygonGroupBuffer);
								PolygonGroupBuffer = NewPolygonGroup(PolygonGroupBuffer);
								PolygonListTail = &PolygonGroupBuffer->PolygonList;
								// FIXME: Should the offset be reset upon reading a 't'?
								//Offset.X = 0;
								//Offset.Y = 0;
								//Offset.Z = 0;
							}
							SuccessfulRead = ReadThickness(&Context, PolygonGroupBuffer);
							break;
						
						// Read the priority
						case 'z':
							// If not currently reading parameters (i.e. not just read a p, z or t) create a new polygon group and add the old one to the list
							if (ReadingParameters == false) {
								ReadingParameters = true;
								AddPolygonGroupToList(&Head, PolygonGroupBuffer);
								PolygonGroupBuffer = NewPolygonGroup(PolygonGroupBuffer);
								PolygonListTail = &PolygonGroupBuffer->PolygonList;
								// FIXME: Should the offset be reset upon reading a 'z'?
								//Offset.X = 0;
								//Offset.Y = 0;
								//Offset.Z = 0;
							}
							SuccessfulRead = ReadPriority(&Context, PolygonGroupBuffer);
							break;
						
						// Read the offset
						case 'o':
							SuccessfulRead = ReadCoordinates(&Context, &Offset);
							break;

						// Read a vertical polygon
						case 'v':
							// Ensure any previous polygons were also vertical
							if (ReadingParameters == false && PolygonGroupBuffer->Type != Vertical) {
								AddPolygonGroupToList(&Head, PolygonGroupBuffer);
								PolygonGroupBuffer = NewPolygonGroup(PolygonGroupBuffer);
								PolygonListTail = &PolygonGroupBuffer->PolygonList;
							}
							ReadingParameters = false;
							PolygonGroupBuffer->Type = Vertical;
							// Allocate memory for a new polygon structure with 2 vertices
							PolygonBuffer = NewPolygon();
							PolygonBuffer->Vertices = (Coordinate*)malloc(2*sizeof(Coordinate));
							// Read in 2 sets of coordinates
							while (ReadCoordinates(&Context, &CoordinateBuffer) == true) {
								// Increment the number of vertices and  ensure only 2 are read for a vertical polygon
								if (PolygonBuffer->nVertices < 2) {
									PolygonBuffer->Vertices[PolygonBuffer->nVertices].X = CoordinateBuffer.X + Offset.X;
									PolygonBuffer->Vertices[PolygonBuffer->nVertices].Y = CoordinateBuffer.Y + Offset.Y;
									PolygonBuffer->Vertices[PolygonBuffer->nVertices].Z = CoordinateBuffer.Z + Offset.Z;
								}	
								PolygonBuffer->nVertices++;
							}
							// Ensure both both vertices have been read
							if (PolygonBuffer->nVertices != 2) {
								SuccessfulRead = false;
							}
							else {
								nPolygons++;
								*PolygonListTail = PolygonBuffer;
								PolygonListTail = &(*PolygonListTail)->NextPolygon;
							}
							break;

						// Read a horizontal polygon
						case 'h':
							// Ensure any previous polygons were also horizontal
							if (ReadingParameters == false && PolygonGroupBuffer->Type != Horizontal) {
								AddPolygonGroupToList(&Head, PolygonGroupBuffer);
								PolygonGroupBuffer = NewPolygonGroup(PolygonGroupBuffer);
								PolygonListTail = &PolygonGroupBuffer->PolygonList;
							}
							ReadingParameters = false;
							PolygonGroupBuffer->Type = Horizontal;
							// Allocate memory for a new polygon structure with 2 vertices
							PolygonBuffer = NewPolygon();
							PolygonBuffer->Vertices = (Coordinate*)malloc(50*sizeof(Coordinate));
							// Read in at least 3 sets of coordinates
							while (ReadCoordinates(&Context, &CoordinateBuffer) == true) {
								// Increment the number of vertices and ensure there is enough memory for all of the vertices, reallocating 10 vertices at a time
								PolygonBuffer->Vertices[PolygonBuffer->nVertices].X = CoordinateBuffer.X + Offset.X;
								PolygonBuffer->Vertices[PolygonBuffer->nVertices].Y = CoordinateBuffer.Y + Offset.Y;
								PolygonBuffer->Vertices[PolygonBuffer->nVertices].Z = CoordinateBuffer.Z + Offset.Z;
								PolygonBuffer->nVertices++;
							}
							// Ensure at least 3 vertices have been read
							if (PolygonBuffer->nVertices < 3) {
								SuccessfulRead = false;
							}
							else {
								nPolygons++;
								*PolygonListTail = PolygonBuffer;
								PolygonListTail = &(*PolygonListTail)->NextPolygon;
							}
							PolygonBuffer->Vertices = (Coordinate*)realloc(PolygonBuffer->Vertices, PolygonBuffer->nVertices*sizeof(Coordinate));
							break;
					}
				}
			}
			else if (feof(SceneFile) != 0) {
				EndOfFile = true;
			}
		}
		while (EndOfFile == false && SuccessfulRead == true);

		// Add the final group to the list
		AddPolygonGroupToList(&Head, PolygonGroupBuffer);
								
		// Free allocated memory
		free(Buffer);
		fclose(SceneFile);

		// Check to see if there were any errors in the scene file
		if (SuccessfulRead == false) {
			printf("Error in scene file, line %d\n", LineCount);
			// Free memory allocated to the polygons
			FreePolygonGroupList(Head);
			return false;
		}
		else {
			printf("Scene file parsed successfully\nRead %d polygons\n\n", nPolygons);
		}
	}

	// Find the maximum coordinates in the system and allocate enough memory for a rectangle of this size
	MaxCoordinates = FindMaxSize(Head);
	// Print information on the polygons to the display
	if (InputData.DisplayPolygonInformation.Flag == true) {
		PrintPolygonGroupList(Head);
	}
	// Allocate memory for the TLM grid
	AllocateGridMemory(MaxCoordinates);
	// Add the polygons into the grid
	AddPolygonsToGrid(Head);
	// Free memory allocated to the polygons
	FreePolygonGroupList(Head);
	// Calculate the reflection and transmission coefficients based on their impedances
	CalculateReflectionTransmissionCoefficients();

	return true;
}
Polygon PolygonLineReduction( Polygon poly, int minr, int maxr ) {
	
	int size = poly->numberOfVertices;
	FVec hist1 = FVecNew(0,size-1);
	
	int range, i, r, l;	
	float x1, y1, x2, y2, x3, y3;
	
	for (range=minr;range<=maxr;range++) {
		for (i=0;i<size;i++) {
		
			x2 = poly->vertices[i].x;
			y2 = poly->vertices[i].y;
		
			r = ( i + range ) % size;
			l = ( i + size - range ) % size;
		
			x1 = poly->vertices[l].x;
			y1 = poly->vertices[l].y;
			x3 = poly->vertices[r].x;
			y3 = poly->vertices[r].y;
		
			float dist = PointToLineDistance2(x1,y1,x3,y3,x2,y2);
			hist1->values[i] += dist;
			
		}
	}
	
	WrapGaussianBlur1D(hist1->values,hist1->l,hist1->r,5);
	FVecDivideBy(hist1,FVecMax(hist1));

	Polygon peaks = NewPolygon(10);
	
	for ( i = 0; i < size ; i++ ) {
		float peak = hist1->values[i];
		if ( peak < 0.3 ) continue;
		l = ( i + size - 1 ) % size;
		r = ( i + 1 ) % size;
		if ( hist1->values[r] > peak ) continue;
		if ( hist1->values[l] > peak ) continue;
		x1 = poly->vertices[i].x;
		y1 = poly->vertices[i].y;
		AddPolygonVertex(peaks,x1,y1);
	}
	
	size = peaks->numberOfVertices;
	for (i=0;i<size;i++) {
		r = ( i + 1 ) % size;
		x1 = peaks->vertices[i].x;
		y1 = peaks->vertices[i].y;
		x2 = peaks->vertices[r].x - x1;
		y2 = peaks->vertices[r].y - y1;
		float dist1 = sqrt( x2*x2 + y2*y2 );
		peaks->vertices[i].z = dist1;
	}
	
	FVecFree(hist1);
	
	return peaks;
	
}
Esempio n. 16
0
// 
// SplitPolygon
//
void SplitPolygon( polygon_t *in, vec3d_t norm, fp_t dist, polygon_t **front, polygon_t **back )
{  
	int		i, j;
	fp_t		dists[MAX_POINTS_ON_POLYGON+1];
	int		sides[MAX_POINTS_ON_POLYGON+1];
	int		counts[3];
	fp_t		d;
	int		maxpts;
	fp_t		*p1, *p2;
	vec3d_t		pclip;
	polygon_t	*f, *b;

	counts[0] = counts[1] = counts[2] = 0;

	for ( i = 0; i < in->pointnum; i++ ) {
		d = Vec3dDotProduct( in->p[i], norm ) - dist;
		dists[i] = d;

		if ( d > front_epsilon )
			sides[i] = SIDE_FRONT;
		else if ( d < back_epsilon )
			sides[i] = SIDE_BACK;
		else 
			sides[i] = SIDE_ON;

		counts[sides[i]]++;
	}

	dists[i] = dists[0];
	sides[i] = sides[0];
	
	*front = *back = NULL;
	
	if ( !counts[SIDE_FRONT] ) 
	{
		*back = CopyPolygon( in );
		return;
	}

	if ( !counts[SIDE_BACK] )
	{
		*front = CopyPolygon( in );
		return;
	}

	maxpts = in->pointnum+4;

	*front = f = NewPolygon( maxpts );
	*back = b = NewPolygon( maxpts );

	for ( i = 0; i < in->pointnum; i++ )
	{
		p1 = in->p[i];
		
		if ( sides[i] == SIDE_ON )
		{
			Vec3dCopy( f->p[f->pointnum++], p1 );
			Vec3dCopy( b->p[b->pointnum++], p1 );
			continue;
		}

		if ( sides[i] == SIDE_FRONT )
			Vec3dCopy( f->p[f->pointnum++], p1 );

		if ( sides[i] == SIDE_BACK )
			Vec3dCopy( b->p[b->pointnum++], p1 );

		if ( sides[i+1] == SIDE_ON || sides[i+1] == sides[i] )
			continue;

		p2 = in->p[(i+1)%in->pointnum];

		d = dists[i] / ( dists[i]-dists[i+1] );
		for ( j = 0; j < 3; j++ )
		{
			if ( norm[j] == 1.0 )
				pclip[j] = dist;
			else if ( norm[j] == -1.0 )
				pclip[j] = -dist;
			else
				pclip[j] = p1[j] + d*(p2[j]-p1[j]);
		}

		Vec3dCopy( f->p[f->pointnum++], pclip );
		Vec3dCopy( b->p[b->pointnum++], pclip );
	}

	if ( f->pointnum > maxpts || b->pointnum > maxpts )
		Error( "maxpts reached.\n" );
}
void GDALRasterPolygonEnumeratorT<DataType,EqualityTest>::ProcessLine(
    DataType *panLastLineVal, DataType *panThisLineVal,
    GInt32 *panLastLineId,  GInt32 *panThisLineId,
    int nXSize )

{
    int i;
    EqualityTest eq;

/* -------------------------------------------------------------------- */
/*      Special case for the first line.                                */
/* -------------------------------------------------------------------- */
    if( panLastLineVal == NULL )
    {
        for( i=0; i < nXSize; i++ )
        {
            if( panThisLineVal[i] == GP_NODATA_MARKER )
            {
                panThisLineId[i] = -1;
            }
            else if( i == 0 || !(eq.operator()(panThisLineVal[i], panThisLineVal[i-1])) )
            {
                panThisLineId[i] = NewPolygon( panThisLineVal[i] );
            }
            else
                panThisLineId[i] = panThisLineId[i-1];
        }

        return;
    }

/* -------------------------------------------------------------------- */
/*      Process each pixel comparing to the previous pixel, and to      */
/*      the last line.                                                  */
/* -------------------------------------------------------------------- */
    for( i = 0; i < nXSize; i++ )
    {
        if( panThisLineVal[i] == GP_NODATA_MARKER )
        {
            panThisLineId[i] = -1;
        }
        else if( i > 0 && eq.operator()(panThisLineVal[i], panThisLineVal[i-1]) )
        {
            panThisLineId[i] = panThisLineId[i-1];

            if( eq.operator()(panLastLineVal[i], panThisLineVal[i])
                && (panPolyIdMap[panLastLineId[i]]
                    != panPolyIdMap[panThisLineId[i]]) )
            {
                MergePolygon( panLastLineId[i], panThisLineId[i] );
            }

            if( nConnectedness == 8
                && eq.operator()(panLastLineVal[i-1], panThisLineVal[i])
                && (panPolyIdMap[panLastLineId[i-1]]
                    != panPolyIdMap[panThisLineId[i]]) )
            {
                MergePolygon( panLastLineId[i-1], panThisLineId[i] );
            }

            if( nConnectedness == 8 && i < nXSize-1
                && eq.operator()(panLastLineVal[i+1], panThisLineVal[i])
                && (panPolyIdMap[panLastLineId[i+1]]
                    != panPolyIdMap[panThisLineId[i]]) )
            {
                MergePolygon( panLastLineId[i+1], panThisLineId[i] );
            }
        }
        else if( eq.operator()(panLastLineVal[i], panThisLineVal[i]) )
        {
            panThisLineId[i] = panLastLineId[i];
        }
        else if( i > 0 && nConnectedness == 8
                 && eq.operator()(panLastLineVal[i-1], panThisLineVal[i]) )
        {
            panThisLineId[i] = panLastLineId[i-1];

            if( i < nXSize-1 && eq.operator()(panLastLineVal[i+1], panThisLineVal[i])
                && (panPolyIdMap[panLastLineId[i+1]]
                != panPolyIdMap[panThisLineId[i]]) )
            {
                MergePolygon( panLastLineId[i+1], panThisLineId[i] );
            }
        }
        else if( i < nXSize-1 && nConnectedness == 8
                 && eq.operator()(panLastLineVal[i+1], panThisLineVal[i]) )
        {
            panThisLineId[i] = panLastLineId[i+1];
        }
        else
            panThisLineId[i] =
                NewPolygon( panThisLineVal[i] );
    }
}
Esempio n. 18
0
void QCSXCAD::BuildToolBar()
{
	QToolBar *mainTB = addToolBar(tr("General"));
	mainTB->setObjectName("General_ToolBar");
	QSize TBIconSize(16,16);
	mainTB->setIconSize(TBIconSize);

	if (QCSX_Settings.GetEdit())
		mainTB->addAction(QIcon(":/images/filenew.png"),tr("New"),this,SLOT(New()));
	if (QCSX_Settings.GetEdit())
		mainTB->addAction(QIcon(":/images/down.png"),tr("Import"),this,SLOT(ImportGeometry()));
	mainTB->addAction(QIcon(":/images/up.png"),tr("Export"),this,SLOT(ExportGeometry()));


	QToolBar *ItemTB = addToolBar(tr("Item View"));
	ItemTB->setIconSize(TBIconSize);
	ItemTB->setObjectName("Item_View_ToolBar");

	ItemTB->addAction(tr("CollapseAll"),CSTree,SLOT(collapseAll()));
	ItemTB->addAction(tr("ExpandAll"),CSTree,SLOT(expandAll()));

	ItemTB->addAction(QIcon(":/images/bulb.png"),tr("ShowAll"),this,SLOT(ShowAll()));
	ItemTB->addAction(QIcon(":/images/bulb_off.png"),tr("HideAll"),this,SLOT(HideAll()));

	QToolBar *newObjct = NULL;
	QAction* newAct = NULL;

	if (QCSX_Settings.GetEdit())
	{
		newObjct = addToolBar(tr("add new Primitive"));
		newObjct->setObjectName("New_Primitive_ToolBar");

		newAct = newObjct->addAction(tr("Box"),this,SLOT(NewBox()));
		newAct->setToolTip(tr("add new Box"));

		newAct = newObjct->addAction(tr("MultiBox"),this,SLOT(NewMultiBox()));
		newAct->setToolTip(tr("add new Multi-Box"));

		newAct = newObjct->addAction(tr("Sphere"),this,SLOT(NewSphere()));
		newAct->setToolTip(tr("add new Sphere"));

		newAct = newObjct->addAction(tr("Cylinder"),this,SLOT(NewCylinder()));
		newAct->setToolTip(tr("add new Cylinder"));

		newAct = newObjct->addAction(tr("Polygon"),this,SLOT(NewPolygon()));
		newAct->setToolTip(tr("add new Polygon"));

		newAct = newObjct->addAction(tr("User Defined"),this,SLOT(NewUserDefined()));
		newAct->setToolTip(tr("add new User Definied Primitive"));

		newObjct = addToolBar(tr("add new Property"));
		newObjct->setObjectName("New_Property_ToolBar");

		newAct = newObjct->addAction(tr("Material"),this,SLOT(NewMaterial()));
		newAct->setToolTip(tr("add new Material-Property"));

		newAct = newObjct->addAction(tr("Metal"),this,SLOT(NewMetal()));
		newAct->setToolTip(tr("add new Metal-Property"));

		newAct = newObjct->addAction(tr("Excitation"),this,SLOT(NewExcitation()));
		newAct->setToolTip(tr("add new Excitation-Property"));

		newAct = newObjct->addAction(tr("ProbeBox"),this,SLOT(NewChargeBox()));
		newAct->setToolTip(tr("add new Probe-Box-Property"));

		newAct = newObjct->addAction(tr("ResBox"),this,SLOT(NewResBox()));
		newAct->setToolTip(tr("add new Res-Box-Property"));

		newAct = newObjct->addAction(tr("DumpBox"),this,SLOT(NewDumpBox()));
		newAct->setToolTip(tr("add new Dump-Box-Property"));
	}

	newObjct = addToolBar(tr("Zoom"));
	newObjct->setIconSize(TBIconSize);
	newObjct->setObjectName("Zoom_ToolBar");

	newAct = newObjct->addAction(QIcon(":/images/viewmagfit.png"),tr("Zoom fit"),this,SLOT(BestView()));
	newAct->setToolTip("Zoom to best fit all objects");

	viewPlane[0] = newObjct->addAction(GridEditor->GetNormName(0),this,SLOT(setYZ()));
	viewPlane[0]->setToolTip(tr("Switch to y-z-plane view (x-normal)"));
	viewPlane[1] = newObjct->addAction(GridEditor->GetNormName(1),this,SLOT(setZX()));
	viewPlane[1]->setToolTip(tr("Switch to z-x-plane view (y-normal)"));
	viewPlane[2] = newObjct->addAction(GridEditor->GetNormName(2),this,SLOT(setXY()));
	viewPlane[2]->setToolTip(tr("Switch to x-y-plane view (z-normal)"));

	addToolBarBreak();

	QActionGroup* ActViewGrp = new QActionGroup(this);
	newAct = newObjct->addAction(tr("2D"),this,SLOT(View2D()));
	newAct->setToolTip(tr("Switch to 2D view mode"));
	ActViewGrp->addAction(newAct);
	newAct->setCheckable(true);
	newAct = newObjct->addAction(tr("3D"),this,SLOT(View3D()));
	newAct->setToolTip(tr("Switch to 3D view mode"));
	ActViewGrp->addAction(newAct);
	newAct->setCheckable(true);
	m_PPview = newObjct->addAction(tr("PP"));
	m_PPview->setToolTip(tr("Toggle parallel projection view mode"));
	QObject::connect(m_PPview,SIGNAL(toggled(bool)),this,SLOT(SetParallelProjection(bool)));
	m_PPview->setCheckable(true);

	if (QCSX_Settings.GetEdit())
		addToolBar(GridEditor->BuildToolbar());
}