public bool IsContained(Vector3D pt) { // Debug2.Push( "IsContained( " + pt + " )" ); if (this.SidePlanes == null || this.SidePlanes.Count != this.Points.Count) { // recreate side planes for Face Vector3D ptNormal = this.GetNormal(); int count = this.Points.Count; this.SidePlanes.Clear(); for (int i = 0; i < count; i++) { Vector3D a = this.Points[(i + count - 1) % count]; Vector3D b = this.Points[i]; Vector3D c = b + ptNormal; this.SidePlanes.Add(Plane3D.FromCoplanarPoints(a, b, c)); } } Debug.Assert(this.SidePlanes.Count == this.Points.Count); bool bInside = true; foreach (Plane3D plane in this.SidePlanes) { // Debug.WriteLine( "plane " + plane + " sign " + plane.GetSign( pt ) ); if (plane.GetSign(pt) > 0) { bInside = false; // Debug.WriteLine( " outside!" ); break; } } // Debug2.Pop(); return(bInside); }
public void LoadDataSet(string fileName, Color clr) { //Debug.Assert( this.Faces.Count == 0 ); //Debug2.Push( "Parse Object Definition File" ); TokenReader tr = new TokenReader(fileName, ' '); int vertexCount = int.Parse(tr.GetToken()); int triangleCount = int.Parse(tr.GetToken()); _vertices = new Vector3D[vertexCount]; _normals = new Vector3D[vertexCount]; _triangles = new int[triangleCount * 3]; _trianglePlanes = new Plane3D[triangleCount]; for (int v = 0; v < vertexCount; v++) { tr.GetToken(); _vertices[v] = new Vector3D( float.Parse(tr.GetToken()), float.Parse(tr.GetToken()), float.Parse(tr.GetToken())); _normals[v] = new Vector3D(0, 0, 0); } // translate vertices Vector3D ptOldMin, ptOldMax; FaceUtils.GetExtents(_vertices, out ptOldMin, out ptOldMax); Vector3D ptOldMidPoint = (ptOldMin + ptOldMax) * 0.5f; float fOldScale = (ptOldMidPoint - ptOldMin).GetMagnitude(); for (int i = 0; i < vertexCount; i++) { _vertices[i] = (_vertices[i] - ptOldMidPoint) / fOldScale * this.Scale + this.MidPoint; } for (int t = 0; t < triangleCount; t++) { tr.GetToken(); // triangle vertices _triangles[t * 3 + 0] = int.Parse(tr.GetToken()); _triangles[t * 3 + 1] = int.Parse(tr.GetToken()); _triangles[t * 3 + 2] = int.Parse(tr.GetToken()); // skip extra triangle info tr.GetToken(); tr.GetToken(); tr.GetToken(); // calculate triangle normal Vector3D vertexA = _vertices[_triangles[t * 3 + 0]]; Vector3D vertexB = _vertices[_triangles[t * 3 + 1]]; Vector3D vertexC = _vertices[_triangles[t * 3 + 2]]; _trianglePlanes[t] = Plane3D.FromCoplanarPoints(vertexA, vertexB, vertexC); //Vector3D.Cross( vertexC - vertexB, vertexA - vertexB ).GetUnit(); } tr.Close(); //Debug2.Pop(); /*for( int v = 0; v < vertexCount; v ++ ) { * if( vertexNormals[ v ].GetMagnitude() > 0.1 ) { * vertexNormals[ v ].Normalize(); * } * else { * vertexNormals[ v ] = new Vector3D( 1, 0, 0 ); * } * } */ //Debug2.Push( "Calculate Face Normals" ); float fLimit = 0f; for (int t = 0; t < triangleCount; t++) { for (int v = 0; v < 3; v++) { int vIndex = _triangles[t * 3 + v]; Vector3D normalSum = _trianglePlanes[t].Normal; Vector3D normal = _trianglePlanes[t].Normal; for (int t2 = 0; t2 < triangleCount; t2++) { if (t2 == t) { continue; } bool bMatch = false; for (int v2 = 0; v2 < 3; v2++) { if (vIndex == _triangles[t2 * 3 + v2]) { bMatch = true; break; } } if (bMatch == true) { Vector3D normal2 = _trianglePlanes[t2].Normal; if (Vector3D.Dot(normal, normal2) > fLimit) { normalSum += normal2; } } } if (normalSum.GetMagnitude() > 0.01) { _normals[vIndex] = normalSum.GetUnit(); } else { _normals[vIndex] = (Vector3D)normal.Clone(); } } } //Debug2.Pop(); /* * int a = faceIndices[ f, 0 ]; * int b = faceIndices[ f, 1 ]; * int c = faceIndices[ f, 2 ]; * * Face face = this.Faces[f]; * face.VertexNormals = new Vector3DCollection(); * face.VertexNormals.Add( vertexNormals[ a ].Clone() ); * face.VertexNormals.Add( vertexNormals[ b ].Clone() ); * face.VertexNormals.Add( vertexNormals[ c ].Clone() ); * }*/ }
public virtual void Reset(World world, object parent) { _resetCount++; if (_resetCount > 1) { //Debug.WriteLine( "Face.Reset() - " + _resetCount ); } // synchronize TextureIndex with actuall index of Texture in World.Textures. if (this.Texture == null) { if (this.TextureIndex != -1) { Debug.Assert(this.TextureIndex < world.Textures.Count); this.Texture = world.Textures[this.TextureIndex]; } else { this.TextureIndex = -1; } } else if (this.Texture != null) { this.TextureIndex = world.Textures.IndexOf(this.Texture); } Debug.Assert((this.Texture == null) == (this.TextureIndex == -1)); if (this.IsTexture) { // ensure bitmap of texture is loaded if (this.Texture.IsBitmapLoaded == false) { this.Texture.LoadBitmap(); } // generate texture coordinates Matrix3D xfrm = GetTextureXfrm(); this.TextureCoords.Clear(); foreach (Vector3D pt in this.Points) { this.TextureCoords.Add(xfrm * pt); } } else { this.TextureCoords.Clear(); } // create Normal and Plane //this.Normal = this.Plane.Normal;//GetNormal(); //this.Plane = this.GetPlane(); //if( this.Plane == null ) { this.Plane = this.GetPlane(); //} // project points onto plane for (int i = 0; i < this.Points.Count; i++) { Vector3D pt = this.Points[i]; float fDistance = this.Plane.GetDistanceToPlane(pt); if (fDistance != 0) { //Debug.Write( "distance[" + i + "] before(" + fDistance + ") -> " ); pt = this.Plane.ProjectOntoPlane(pt); this.Points[i] = pt; //Debug.WriteLine( "after(" + this.Plane.GetDistanceToPlane( pt ) + ")" ); } } // recreate side planes for Face Vector3D ptNormal = this.Normal; int count = this.Points.Count; this.SidePlanes.Clear(); for (int i = 0; i < count; i++) { Vector3D a = this.Points[(i + count - 1) % count]; Vector3D b = this.Points[i]; Vector3D c = b + ptNormal; this.SidePlanes.Add(Plane3D.FromCoplanarPoints(a, b, c)); } // this.IsVertexNormals = (this.VertexNormals.Count > 0); if (this.IsVertexNormals) { Debug.Assert(this.VertexNormals.Count == this.Points.Count); } // compute face midpoint this.MidPoint = this.GetMidPoint(); }
static public WorldcraftSidePlane FromTokenReader(TokenReader tr, Textures textures) { WorldcraftSidePlane sp = new WorldcraftSidePlane(); Debug.Assert(tr.LookAhead == "("); tr.GetToken(); Vector3D pt1 = ReadVector3D(tr); Debug.Assert(tr.LookAhead == ")"); tr.GetToken(); Debug.Assert(tr.LookAhead == "("); tr.GetToken(); Vector3D pt2 = ReadVector3D(tr); Debug.Assert(tr.LookAhead == ")"); tr.GetToken(); Debug.Assert(tr.LookAhead == "("); tr.GetToken(); Vector3D pt3 = ReadVector3D(tr); Debug.Assert(tr.LookAhead == ")"); tr.GetToken(); sp.plane = Plane3D.FromCoplanarPoints(pt2, pt1, pt3); sp.plane.Flip(); //Debug.Assert( sp.plane != null ); sp.texture = textures.RequestTexture(tr.GetToken()); Debug.Assert(sp.texture != null); if (sp.texture.IsBitmapLoaded == false) { sp.texture.LoadBitmap(); } Debug.Assert(tr.LookAhead == "["); tr.GetToken(); Vector3D sAxis = ReadVector3D(tr); float sOffset = float.Parse(tr.GetToken()); Debug.Assert(tr.LookAhead == "]"); tr.GetToken(); Debug.Assert(tr.LookAhead == "["); tr.GetToken(); Vector3D tAxis = ReadVector3D(tr); float tOffset = float.Parse(tr.GetToken()); Debug.Assert(tr.LookAhead == "]"); tr.GetToken(); Vector3D rAxis = Vector3D.Cross(sAxis, tAxis); float rotation = float.Parse(tr.GetToken()); float sScale = float.Parse(tr.GetToken()); float tScale = float.Parse(tr.GetToken()); Matrix3D xfrm = new Matrix3D(); //xfrm.Scale( sScale, tScale, 1 ); //xfrm.Translate( -sOffset, -tOffset, 0 ); sp.sAxis = sAxis; sp.tAxis = tAxis; //xfrm.ChangeBasis( sAxis, tAxis, rAxis ); //xfrm.Scale( sp.texture.Width, sp.texture.Height, 1 ); //xfrm.Invert(); //sp.xfrm = xfrm; return(sp); }