MeshNormalSpec.h

Go to the documentation of this file.
00001 /**********************************************************************
00002  *<
00003     FILE: MeshNormalSpec.h
00004 
00005     DESCRIPTION:  User-specifiable normals for Triangle Meshes
00006 
00007     CREATED BY: Steve Anderson
00008 
00009     HISTORY: created July 2002 during SIGGraph
00010 
00011  *> Copyright (c) 2002 Autodesk, Inc., All Rights Reserved.
00012  **********************************************************************/
00013 
00014 // Necessary prior inclusions...?
00015 
00016 #pragma once
00017 #include "export.h"
00018 #include "maxheap.h"
00019 #include "baseinterface.h"
00020 #include "ipipelineclient.h"
00021 #include "bitarray.h"
00022 #include "box3.h"
00023 
00024 // forward declarations
00025 class ISave;
00026 class ILoad;
00027 class Point3;
00028 class Mesh;
00029 class GraphicsWindow;
00030 class HitRegion;
00031 class SubObjHitList;
00032 
00033 #define MESH_NORMAL_SPEC_INTERFACE Interface_ID(0xa4e770b, 0x47aa3cf9)
00034 
00047 class MeshNormalFace: public MaxHeapOperators {
00048     int mNormalID[3];
00049     UBYTE mSpecified;
00050 
00051 public:
00053     MeshNormalFace () : mSpecified(0x0) { }
00054 
00056     DllExport void Clear();
00057 
00061     // used in that corner, or -1 for error 
00062     int GetNormalID(int corner) { return ((corner>-1) && (corner<3)) ? mNormalID[corner] : -1; }
00063 
00067     void SetNormalID (int corner, int norm) { if ((corner>-1) && (corner<3)) mNormalID[corner] = norm; }
00068 
00071     int *GetNormalIDArray () { return mNormalID; }
00072 
00074     bool GetSpecified (int corner) { return ((corner>-1) && (corner<3) && (mSpecified & (1<<corner))) ? true : false; }
00075 
00077     DllExport void SetSpecified (int corner, bool value=true);
00078 
00080     DllExport void SpecifyNormalID (int corner, int norm);
00081 
00083     void SpecifyAll (bool value=true) { mSpecified = value ? 7 : 0; }
00084 
00087     DllExport void Flip ();
00088 
00090     DllExport MeshNormalFace & operator= (const MeshNormalFace & from);
00091 
00096     DllExport void MyDebugPrint (bool printAll=false);
00097 
00099     DllExport IOResult Save (ISave *isave);
00100 
00102     DllExport IOResult Load (ILoad *iload);
00103 };
00104 
00105 // Class MeshNormalSpec flags:
00106 #define MESH_NORMAL_NORMALS_BUILT 0x01
00107 #define MESH_NORMAL_NORMALS_COMPUTED 0x02
00108 #define MESH_NORMAL_DISPLAY_HANDLES 0x04
00109 #define MESH_NORMAL_FACE_ANGLES 0x08    // Used face angles last time we computed normals
00110 #define MESH_NORMAL_MODIFIER_SUPPORT 0x20   // When modifying mesh, indicates whether the current modifier supports these Normals.
00111 
00112 // Default length for normal display
00113 #define MESH_NORMAL_LENGTH_DEFAULT 10.0f
00114 
00142 class MeshNormalSpec : public IPipelineClient {
00143 private:
00144     int mNumNormalAlloc, mNumFaceAlloc;
00145     int mNumNormals, mNumFaces;
00146     MeshNormalFace *mpFace;
00147     Point3 *mpNormal;
00148     BitArray mNormalExplicit;   // Indicates whether mpNormal[i] is explicit or computed from face normals.
00149 
00150     // Display and selection data:
00151     BitArray mNormalSel;
00152     float mDisplayLength;
00153     DWORD mFlags;
00154 
00155     // We also maintain a pointer to the parent Mesh
00156     // (NOTE that the Mesh MUST keep this pointer updated at all times!)
00157     Mesh *mpParent;
00158 
00159 public:
00160     MeshNormalSpec () : mpFace(NULL), mpNormal(NULL), mNumNormalAlloc(0),
00161         mNumFaceAlloc(0), mNumNormals(0), mNumFaces(0), mpParent(NULL), mFlags(0),
00162         mDisplayLength(MESH_NORMAL_LENGTH_DEFAULT) { }
00163     ~MeshNormalSpec () { ClearAndFree (); }
00164 
00167     void SetFlag (DWORD fl, bool val=true) { if (val) mFlags |= fl; else mFlags &= ~fl; }
00168 
00170     void ClearFlag (DWORD fl) { mFlags &= ~fl; }
00171 
00187     bool GetFlag (DWORD fl) const { return (mFlags & fl) ? true : false; }
00188 
00189     // Initialization, allocation:
00190     
00192     DllExport void Initialize ();
00193     DllExport bool NAlloc (int num, bool keep=TRUE);
00195     DllExport void NShrink ();
00196     DllExport bool FAlloc (int num, bool keep=TRUE);
00197     DllExport void FShrink ();
00199     DllExport void Clear ();
00201     DllExport void ClearAndFree (); 
00202 
00203     // Data access:
00204     // Lowest level:
00205     int GetNumFaces () const { return mNumFaces; }
00206     DllExport bool SetNumFaces (int numFaces);
00207     int GetNumNormals () const { return mNumNormals; }
00208     DllExport bool SetNumNormals (int numNormals);
00209 
00210     Point3 & Normal (int normID) const { return mpNormal[normID]; }
00211     Point3 * GetNormalArray () const { return mpNormal; }
00212     bool GetNormalExplicit (int normID) const { return mNormalExplicit[normID] ? true : false; }
00213     void SetNormalExplicit (int normID, bool value) { mNormalExplicit.Set (normID, value); }
00214 
00217     void SetAllExplicit (bool value=true) { if (value) mNormalExplicit.SetAll(); else mNormalExplicit.ClearAll (); }
00218     MeshNormalFace & Face(int faceID) const { return mpFace[faceID]; }
00219     MeshNormalFace * GetFaceArray () const { return mpFace; }
00220 
00221     void SetParent (Mesh *pMesh) { mpParent = pMesh; }
00222 
00223     // Data access - higher level:
00224     DllExport Point3 & GetNormal (int face, int corner);
00225     DllExport void SetNormal (int face, int corner, Point3 & normal);
00226     DllExport int GetNormalIndex (int face, int corner);
00227     DllExport void SetNormalIndex (int face, int corner, int normalIndex);
00228     DllExport int NewNormal (Point3 normal, bool explic=true);
00229 
00230     DllExport void SetSelection (BitArray & newSelection);
00231     BitArray & GetSelection() { return mNormalSel; }
00232     void SetDisplayLength (float displayLength) { mDisplayLength = displayLength; }
00233     float GetDisplayLength () { return mDisplayLength; }
00234 
00235     // Display and hit testing - note that these require an accurate mpParent pointer.
00236     DllExport void Display (GraphicsWindow *gw, bool showSel);
00237     DllExport bool HitTest (GraphicsWindow *gw, HitRegion *hr, DWORD flags, SubObjHitList& hitList);
00238     DllExport Box3 GetBoundingBox (Matrix3 *tm=NULL, bool selectedOnly=false);
00239 
00241     DllExport void ClearNormals ();
00242 
00247     DllExport void BuildNormals ();
00248 
00252     DllExport void ComputeNormals ();
00253 
00256     DllExport void CheckNormals ();
00257 
00258     // operators and debug printing
00259     DllExport MeshNormalSpec & operator= (const MeshNormalSpec & from);
00260     DllExport void CopySpecified (const MeshNormalSpec & from); // Like operator=, but omits unspecified.
00261     DllExport MeshNormalSpec & operator+= (const MeshNormalSpec & from);
00262     DllExport void MyDebugPrint (bool printAll=false);
00263     DllExport bool CheckAllData (int numParentFaces);
00264 
00265     DllExport IOResult Save (ISave *isave);
00266     DllExport IOResult Load (ILoad *iload);
00267 
00268     // From BaseInterface:
00269     Interface_ID GetID() {return MESH_NORMAL_SPEC_INTERFACE;}
00270     DllExport void DeleteInterface();
00271     DllExport BaseInterface* GetInterface(Interface_ID id);
00272     DllExport BaseInterface* CloneInterface(void* remapDir = NULL);
00273 
00274     // --- IPipelineClient methods
00275     DllExport void ShallowCopy( IPipelineClient* from, ChannelMask channels );
00276     DllExport void DeepCopy( IPipelineClient* from, ChannelMask channels );
00277     DllExport void NewAndCopyChannels( ChannelMask channels );
00278     DllExport void FreeChannels( ChannelMask channels, int zeroOthers = 1 );
00279     DllExport void ZeroChannels( ChannelMask channels );
00280     DllExport void AppendAllChannels( IPipelineClient* from );
00281 
00282     // Actual operations:
00283     DllExport bool Transform (Matrix3 & xfm, BOOL useSel=false, BitArray *normalSelection=NULL);
00284     DllExport bool Translate (Point3 & translate, BOOL useSel=true, BitArray *normalSelection=NULL);
00285     DllExport bool BreakNormals (BOOL useSel=true, BitArray *normalSelection=NULL, BOOL toAverage=false);
00286     // Requires an accurate mpParent pointer:
00287     DllExport bool UnifyNormals (BOOL useSel=true, BitArray *normalSelection=NULL, BOOL toAverage=false);
00288     DllExport bool AverageNormals (BOOL useThresh=false, float threshold=0.0f, BOOL useSel=true, BitArray *normalSelection=NULL);
00289     DllExport bool SpecifyNormals (BOOL useSel=true, BitArray *normalSelection=NULL);
00290     DllExport bool MakeNormalsExplicit (BOOL useSel=true, BitArray *normalSelection=NULL, bool value=true);
00291     DllExport bool ResetNormals (BOOL useSel=true, BitArray *normalSelection=NULL);
00292 };
00293