HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GU_Flatten.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: GU Library (C++)
7  *
8  * COMMENTS: Flatten GU
9  */
10 
11 #ifndef __GU_Flatten_h__
12 #define __GU_Flatten_h__
13 
14 #include "GU_API.h"
15 #include "GU_Detail.h"
16 #include <GA/GA_Edge.h>
17 #include <GA/GA_EdgeGroup.h>
18 #include <UT/UT_StringStream.h>
19 #include <UT/UT_Map.h>
20 #include <GEO/GEO_HedgeInterface.h>
21 
22 class UT_Classifier;
23 
24 #define PROJECTED_INITIAL_VALUES 0
25 
26 class UT_AutoInterrupt;
27 
29 {
30 public:
31  GU_Flatten(GU_Detail *gdp,
32  const GA_PrimitiveGroup *prims,
33  const GA_EdgeGroup *seams,
34  const GA_EdgeGroup *extra_seams,
35  bool use_input_uv = false,
36  bool axis_align_islands = false,
37  bool repack_wasted = false);
38 
39  ~GU_Flatten();
40 
42  { return myNumIslands; }
43 
44  /// Updates UVs in myTGdp and packs all islands into unit square.
45  /// Once finished, myTGdp will hold UVs as created after flattening
46  /// with the initially assigned pins.
47  /// Returns true if interrupted and false otherwise
48  bool flattenAndPack();
49 
50  /// Re-flattens islands affected by changes in current pins.
51  /// Returns 1 if was interrupted and 0 otherwise
52  int reflattenWithPins();
53 
54  /// Creates a vertex attribute for angular distortion and computes the
55  /// distortion for all islands. It also creates the input vertex
56  /// equivalence classes (myInputVertexClasses).
57  void addDistortionAttribute();
58 
59  // Adds an vertex attribute called "uvpointid" mapping vertices of myGdp to
60  // the corresponding point indices of myRGdp; this is used in MPI and MSS
61  void addPointIdAttribute();
62 
63  /// Creates a primitive attribute for island number
64  void addIslandAttribute(bool add);
65 
66  /// Change the extra cuts edge group
67  void updateExtraSeamsGroup(const GA_EdgeGroup *exgp);
68 
69  /// Change the primitive group
70  void updatePrimitiveGroup(const GA_PrimitiveGroup *prims);
71 
72  /// Add one pinned point
73  void addPin(int prim, int vert, fpreal u, fpreal v);
74 
75  /// Create a vertex group of all original pins.
76  void addOriginalPinGroup(const char *name);
77 
78  /// Create a vertex group of all pins used during the flattening, both
79  /// initial and user-specified.
80  void addActivePinGroup(const char *name);
81 
82  void addSeamsGroup(const char *name);
83 
84  // The class encapsulating a pin
85  class Pin
86  {
87  public:
88  Pin() :
89  myInputVertex(GA_INVALID_OFFSET), myPoint(GA_INVALID_OFFSET),
90  myIslandIndex(-1), myU(0.0), myV(0.0), myToRemove(false) {}
91 
92  Pin(GA_Offset voff, GA_Offset ptof, int is, fpreal u, fpreal v) :
93  myInputVertex(voff), myPoint(ptof), myIslandIndex(is),
94  myU(u), myV(v), myToRemove(false) {}
95 
96  Pin(const Pin &other) :
97  myInputVertex(other.myInputVertex), myPoint(other.myPoint),
98  myIslandIndex(other.myIslandIndex), myU(other.myU), myV(other.myV),
99  myToRemove(other.myToRemove) {}
100 
101  GA_Offset getInputVertex() const { return myInputVertex; }
102  GA_Offset getPoint() const { return myPoint; }
103  inline void setU(fpreal u) { myU = u; }
104  inline void setV(fpreal v) { myV = v; }
105  inline fpreal u() const { return myU; }
106  inline fpreal v() const { return myV; }
107  inline int getIslandIndex() const { return myIslandIndex; }
108  inline void setToRemove(bool b) { myToRemove = b; }
109  inline bool isToRemove() const { return myToRemove; }
110  private:
111  GA_Offset myInputVertex; // vertex offset in myGdp
112  GA_Offset myPoint; // point offset in myRGdp for regular
113  // pins or in myTGdp for "original" pins
114  fpreal myU, myV;
115  bool myToRemove;
116  int myIslandIndex;
117  };
118 
120  { return myWarningMessage.str().toStdString(); }
121 private:
122 
123  void buildIslands(GU_Detail *gdp, int num_islands);
124 
125  class Island
126  {
127  public:
128  Island(GU_Detail *gdp, int island_id, bool pos_from_uv = false);
129 
130  GU_Detail *getDetail() { return myGdp; }
131  int getIndex() const { return myIndex; }
132  GA_RWHandleV3 &getUVHandle() { return myUV; }
133 
134  const
135  GA_OffsetArray &getPoints() { return myPoints; }
136  const
137  GA_OffsetArray &getPrimitives() { return myPrimitives; }
138  const
139  GA_OffsetArray &getInputPrimitives() { return myInputPrimitives; }
140 
141  inline
142  UT_Vector3 getPos3(GA_Offset pt) const;
143 
144  inline void appendPrimitive(GA_Offset primoff)
145  { myPrimitives.append(primoff); }
146  inline void appendPoint(GA_Offset ptoff)
147  { myPoints.append(ptoff); }
148  inline void appendInputPrimitive(GA_Offset primoff)
149  { myInputPrimitives.append(primoff); }
150 
151 #if PROJECTED_INITIAL_VALUES
152  void setCenter(UT_Vector3 ctr) { myCenter = ctr; }
153  UT_Vector3 getCenter() const { return myCenter; }
154 
155  void setXAxis(UT_Vector3 ctr) { myXAxis = ctr; }
156  UT_Vector3 getXAxis() const { return myXAxis; }
157 
158  void setYAxis(UT_Vector3 ctr) { myYAxis = ctr; }
159  UT_Vector3 getYAxis() const { return myYAxis; }
160 
161  void setWidth(fpreal w) { myWidth = w; }
162  fpreal getWidth() const { return myWidth; }
163 #endif
164 
165  private:
166  GA_OffsetArray myPrimitives;
167  GA_OffsetArray myPoints;
168  GA_OffsetArray myInputPrimitives;
169 
170 #if PROJECTED_INITIAL_VALUES
171  UT_Vector3 myXAxis, myYAxis, myCenter;
172  fpreal myWidth;
173 #endif
174 
175  GU_Detail *myGdp;
176  int myIndex;
177  bool myPosFromUV;
178  GA_RWHandleV3 myUV;
179  GA_ROHandleV3 myOrigUV;
180  };
181 
182  typedef UT_Array<Island *> IslandArray;
183  typedef UT_Array<Pin> PinArray;
184  typedef UT_Map<int, PinArray> PinMap;
185 
186  void calculateDistortion(GA_RWHandleF &disth,
187  Island &island);
188 
189  /// Re-flatten an island using current pins
190  void flattenIsland(Island &island,
191  UT_AutoInterrupt &boss);
192 
193  /// Flatten an island using the auto-assigned initial pins
194  void flattenIslandWithInitialPins(Island &island,
195  UT_AutoInterrupt &boss);
196 
197  /// The main flattening method; flattens the given island with the given
198  /// pins. Returns true if interrupted and false otherwise.
199  void flattenLSCM(Island &island, const PinArray &pins,
200  bool write_to_input_detail,
201  UT_AutoInterrupt &boss);
202 
203  /// Translate UVs of an island by vector t
204  void translateIsland(UT_Vector3 t, Island &island,
205  bool write_to_input_detail = true);
206 
207  /// Attach symmetric pins to each original island. Fill myOriginalPins
208  void assignIslandInitialPins(Island &island);
209 
210  /// Construct myRGdp by cutting myTGdp along extra edges
211  void recutWithExtraSeams(bool force_rebuild_islands = false);
212 
213  void writeUVsToInputDetail(GA_ROHandleV3 &uvh);
214 
215  /// Returns a representative input vertex for a given point in myTGdp
216  inline GA_Offset workPointToInputVertex(GA_Offset work_pt)
217  { return myWorkPointToInputVertex(work_pt); }
218 
219  /// Returns a representative vertex in the myTGdp (equivalently myRGdp)
220  // for the given input vertex
221  inline GA_Offset getWorkVertex(GA_Offset vtx)
222  { return myWorkVertex(vtx); }
223 
224  inline GA_Offset getInputVertex(GA_Offset vtx)
225  { return myInputVertex(vtx); }
226 
227  void getIslandPins(PinMap &pin_map, int island,
228  PinArray &pins);
229 
230  int cutDetailAlongSeams(GU_Detail *gdp,
231  const GEO_DetachedHedgeInterface *hip,
232  const GA_EdgeGroup *seams,
233  GU_Detail *seams_gdp,
234  GA_RWHandleI &islandh);
235 
236  enum PinStatus
237  {
238  NEW_PIN,
239  DUPLICATE_PIN,
240  MODIFIED_PIN
241  };
242 
243  PinStatus addPinToPinMap(GU_Flatten::PinMap &pin_map, Pin pin);
244  void processPins();
245 
246  /// map an input vertex to a work point
247  inline GA_Offset getWorkPoint(GA_Offset in_vtx)
248  {
249  GA_Offset vtx = getWorkVertex(in_vtx);
250  if (!GAisValid(vtx))
251  return GA_INVALID_OFFSET;
252  return myRGdp->vertexPoint(vtx);
253  }
254 
255  GA_Attribute *getTextureAttribute(GU_Detail *gdp,
256  bool add_if_missing = true);
257  GA_Attribute *getIslandAttribute(GU_Detail *gdp,
258  bool add_if_missing = true);
259 
260  void purgeIslands(int new_num_islands = 0);
261  Island *getIsland(int i, GU_Detail *gdp);
262  void buildInputVertexClasses();
263 
265  *getInputHedgeInterface();
266 
267  // We compute a partitioning of input vertices into equivalence classes
268  // for computation of distortion.
269  // Two vertices are placed in the same equivalence class if and only if
270  // they have the same 3D position and the same assigned UV coordinates.
271  // The root of each class is used for the purpose of accumulation of
272  // distortion amounts.
273 
274  UT_Classifier *myInputVertexClasses;
275 
276  PinMap myInitialPins;
277  PinMap myCurrentPins;
278 
279  GA_OffsetArray myWorkPointToInputVertex;
280 
281  GA_OffsetArray myWorkVertex;
282  GA_OffsetArray myInputVertex;
283 
284  GU_Detail *myGdp; // input detail
285  GU_Detail *myTGdp; // triangulated and cut detail
286  GU_Detail *myRGdp; // re-cut (additional seams) detail
287 
288  GA_RWHandleV3 myUV;
289  GA_RWHandleV3 myTUV;
290  GA_RWHandleV3 myRUV; // attribute handle on myRGdp
291  GA_RWHandleI myPrimitiveIsland; // attribute handle on myRGdp
292 
293  UT_Array<bool> myReflattenIslandFlags;
294  UT_BitArray myIslandsToReflatten;
295 
296  GA_EdgeGroup *myInitialSeams;
297  GA_EdgeGroup *myExtraSeams;
298 
299  int myInitialNumIslands;
300  int myNumIslands;
301  IslandArray myIslands;
302 
303  fpreal myTotalArea; // used for scaling
304 
305  bool myUseInputUV;
306  bool myAxisAlignIslands;
307  bool myRepackWasted;
308  UT_StringStream myWarningMessage;
309 
310  GEO_DetachedHedgeInterface *myInputHI;
311  GEO_DetachedHedgeInterface *myHip; // on myTTGdp
312  GA_PrimitiveGroup *myGroup; // input primitive group
313 };
314 
315 
317 GU_Flatten::Island::getPos3(GA_Offset pt) const
318 {
319  if (myPosFromUV)
320  return myOrigUV.get(myGdp->pointVertex(pt));
321  else
322  return myGdp->getPos3(pt);
323 }
324 
325 #endif
Definition of a geometry attribute.
Definition: GA_Attribute.h:189
GA_Offset getPoint() const
Definition: GU_Flatten.h:102
const GLdouble * v
Definition: glcorearb.h:836
GLsizei const GLchar *const * string
Definition: glcorearb.h:813
bool GAisValid(GA_Size v)
Definition: GA_Types.h:625
int getNumIslands()
Definition: GU_Flatten.h:41
GU_API Status flattenIsland(Method method, const Island &island, const ConstraintSet &constraints, RWHandleV3R uvh)
3D Vector class.
png_uint_32 i
Definition: png.h:2877
bool isToRemove() const
Definition: GU_Flatten.h:109
#define GA_INVALID_OFFSET
Definition: GA_Types.h:654
GA_Size GA_Offset
Definition: GA_Types.h:617
An bi-directional stream object that owns its own string buffer storage.
const std::string getWarningMessage()
Definition: GU_Flatten.h:119
#define GU_API
Definition: GU_API.h:11
GA_Offset getInputVertex() const
Definition: GU_Flatten.h:101
GLuint const GLchar * name
Definition: glcorearb.h:785
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1221
Pin(const Pin &other)
Definition: GU_Flatten.h:96
double fpreal
Definition: SYS_Types.h:269
void setU(fpreal u)
Definition: GU_Flatten.h:103
fpreal v() const
Definition: GU_Flatten.h:106
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:856
Pin(GA_Offset voff, GA_Offset ptof, int is, fpreal u, fpreal v)
Definition: GU_Flatten.h:92
int getIslandIndex() const
Definition: GU_Flatten.h:107
void setToRemove(bool b)
Definition: GU_Flatten.h:108
fpreal u() const
Definition: GU_Flatten.h:105
void setV(fpreal v)
Definition: GU_Flatten.h:104