HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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  GA_Offset getInputVertex() const { return myInputVertex; }
97  GA_Offset getPoint() const { return myPoint; }
98  inline void setU(fpreal u) { myU = u; }
99  inline void setV(fpreal v) { myV = v; }
100  inline fpreal u() const { return myU; }
101  inline fpreal v() const { return myV; }
102  inline int getIslandIndex() const { return myIslandIndex; }
103  inline void setToRemove(bool b) { myToRemove = b; }
104  inline bool isToRemove() const { return myToRemove; }
105  private:
106  GA_Offset myInputVertex; // vertex offset in myGdp
107  GA_Offset myPoint; // point offset in myRGdp for regular
108  // pins or in myTGdp for "original" pins
109  fpreal myU, myV;
110  bool myToRemove;
111  int myIslandIndex;
112  };
113 
115  { return myWarningMessage.str().toStdString(); }
116 private:
117 
118  void buildIslands(GU_Detail *gdp, int num_islands);
119 
120  class Island
121  {
122  public:
123  Island(GU_Detail *gdp, int island_id, bool pos_from_uv = false);
124 
125  GU_Detail *getDetail() { return myGdp; }
126  int getIndex() const { return myIndex; }
127  GA_RWHandleV3 &getUVHandle() { return myUV; }
128 
129  const
130  GA_OffsetArray &getPoints() { return myPoints; }
131  const
132  GA_OffsetArray &getPrimitives() { return myPrimitives; }
133  const
134  GA_OffsetArray &getInputPrimitives() { return myInputPrimitives; }
135 
136  inline
137  UT_Vector3 getPos3(GA_Offset pt) const;
138 
139  inline void appendPrimitive(GA_Offset primoff)
140  { myPrimitives.append(primoff); }
141  inline void appendPoint(GA_Offset ptoff)
142  { myPoints.append(ptoff); }
143  inline void appendInputPrimitive(GA_Offset primoff)
144  { myInputPrimitives.append(primoff); }
145 
146 #if PROJECTED_INITIAL_VALUES
147  void setCenter(UT_Vector3 ctr) { myCenter = ctr; }
148  UT_Vector3 getCenter() const { return myCenter; }
149 
150  void setXAxis(UT_Vector3 ctr) { myXAxis = ctr; }
151  UT_Vector3 getXAxis() const { return myXAxis; }
152 
153  void setYAxis(UT_Vector3 ctr) { myYAxis = ctr; }
154  UT_Vector3 getYAxis() const { return myYAxis; }
155 
156  void setWidth(fpreal w) { myWidth = w; }
157  fpreal getWidth() const { return myWidth; }
158 #endif
159 
160  private:
161  GA_OffsetArray myPrimitives;
162  GA_OffsetArray myPoints;
163  GA_OffsetArray myInputPrimitives;
164 
165 #if PROJECTED_INITIAL_VALUES
166  UT_Vector3 myXAxis, myYAxis, myCenter;
167  fpreal myWidth;
168 #endif
169 
170  GU_Detail *myGdp;
171  int myIndex;
172  bool myPosFromUV;
173  GA_RWHandleV3 myUV;
174  GA_ROHandleV3 myOrigUV;
175  };
176 
177  typedef UT_Array<Island *> IslandArray;
178  typedef UT_Array<Pin> PinArray;
179  typedef UT_Map<int, PinArray> PinMap;
180 
181  void calculateDistortion(GA_RWHandleF &disth,
182  Island &island);
183 
184  /// Re-flatten an island using current pins
185  void flattenIsland(Island &island,
186  UT_AutoInterrupt &boss);
187 
188  /// Flatten an island using the auto-assigned initial pins
189  void flattenIslandWithInitialPins(Island &island,
190  UT_AutoInterrupt &boss);
191 
192  /// The main flattening method; flattens the given island with the given
193  /// pins. Returns true if interrupted and false otherwise.
194  void flattenLSCM(Island &island, const PinArray &pins,
195  bool write_to_input_detail,
196  UT_AutoInterrupt &boss);
197 
198  /// Translate UVs of an island by vector t
199  void translateIsland(UT_Vector3 t, Island &island,
200  bool write_to_input_detail = true);
201 
202  /// Attach symmetric pins to each original island. Fill myOriginalPins
203  void assignIslandInitialPins(Island &island);
204 
205  /// Construct myRGdp by cutting myTGdp along extra edges
206  void recutWithExtraSeams(bool force_rebuild_islands = false);
207 
208  void writeUVsToInputDetail(GA_ROHandleV3 &uvh);
209 
210  /// Returns a representative input vertex for a given point in myTGdp
211  inline GA_Offset workPointToInputVertex(GA_Offset work_pt)
212  { return myWorkPointToInputVertex(work_pt); }
213 
214  /// Returns a representative vertex in the myTGdp (equivalently myRGdp)
215  // for the given input vertex
216  inline GA_Offset getWorkVertex(GA_Offset vtx)
217  { return myWorkVertex(vtx); }
218 
219  inline GA_Offset getInputVertex(GA_Offset vtx)
220  { return myInputVertex(vtx); }
221 
222  void getIslandPins(PinMap &pin_map, int island,
223  PinArray &pins);
224 
225  int cutDetailAlongSeams(GU_Detail *gdp,
227  const GA_EdgeGroup *seams,
228  GU_Detail *seams_gdp,
229  GA_RWHandleI &islandh);
230 
231  enum PinStatus
232  {
233  NEW_PIN,
234  DUPLICATE_PIN,
235  MODIFIED_PIN
236  };
237 
238  PinStatus addPinToPinMap(GU_Flatten::PinMap &pin_map, Pin pin);
239  void processPins();
240 
241  /// map an input vertex to a work point
242  inline GA_Offset getWorkPoint(GA_Offset in_vtx)
243  {
244  GA_Offset vtx = getWorkVertex(in_vtx);
245  if (!GAisValid(vtx))
246  return GA_INVALID_OFFSET;
247  return myRGdp->vertexPoint(vtx);
248  }
249 
250  GA_Attribute *getTextureAttribute(GU_Detail *gdp,
251  bool add_if_missing = true);
252  GA_Attribute *getIslandAttribute(GU_Detail *gdp,
253  bool add_if_missing = true);
254 
255  void purgeIslands(int new_num_islands = 0);
256  Island *getIsland(int i, GU_Detail *gdp);
257  void buildInputVertexClasses();
258 
260  *getInputHedgeInterface();
261 
262  // We compute a partitioning of input vertices into equivalence classes
263  // for computation of distortion.
264  // Two vertices are placed in the same equivalence class if and only if
265  // they have the same 3D position and the same assigned UV coordinates.
266  // The root of each class is used for the purpose of accumulation of
267  // distortion amounts.
268 
269  UT_Classifier *myInputVertexClasses;
270 
271  PinMap myInitialPins;
272  PinMap myCurrentPins;
273 
274  GA_OffsetArray myWorkPointToInputVertex;
275 
276  GA_OffsetArray myWorkVertex;
277  GA_OffsetArray myInputVertex;
278 
279  GU_Detail *myGdp; // input detail
280  GU_Detail *myTGdp; // triangulated and cut detail
281  GU_Detail *myRGdp; // re-cut (additional seams) detail
282 
283  GA_RWHandleV3 myUV;
284  GA_RWHandleV3 myTUV;
285  GA_RWHandleV3 myRUV; // attribute handle on myRGdp
286  GA_RWHandleI myPrimitiveIsland; // attribute handle on myRGdp
287 
288  UT_Array<bool> myReflattenIslandFlags;
289  UT_BitArray myIslandsToReflatten;
290 
291  GA_EdgeGroupUPtr myInitialSeams;
292  GA_EdgeGroupUPtr myExtraSeams;
293 
294  int myInitialNumIslands;
295  int myNumIslands;
296  IslandArray myIslands;
297 
298  fpreal myTotalArea; // used for scaling
299 
300  bool myUseInputUV;
301  bool myAxisAlignIslands;
302  bool myRepackWasted;
303  UT_StringStream myWarningMessage;
304 
305  GEO_DetachedHedgeInterface *myInputHI;
306  GEO_DetachedHedgeInterface *myHip; // on myTTGdp
307  GA_PrimitiveGroupUPtr myGroup; // input primitive group
308 };
309 
310 
312 GU_Flatten::Island::getPos3(GA_Offset pt) const
313 {
314  if (myPosFromUV)
315  return myOrigUV.get(myGdp->pointVertex(pt));
316  else
317  return myGdp->getPos3(pt);
318 }
319 
320 #endif
Definition of a geometry attribute.
Definition: GA_Attribute.h:198
GA_Offset getPoint() const
Definition: GU_Flatten.h:97
const GLdouble * v
Definition: glcorearb.h:837
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
int getNumIslands()
Definition: GU_Flatten.h:41
SYS_FORCE_INLINE bool GAisValid(GA_Size v)
Definition: GA_Types.h:655
bool isToRemove() const
Definition: GU_Flatten.h:104
#define GA_INVALID_OFFSET
Definition: GA_Types.h:687
GA_Size GA_Offset
Definition: GA_Types.h:646
An bi-directional stream object that owns its own string buffer storage.
const std::string getWarningMessage()
Definition: GU_Flatten.h:114
UT_UniquePtr< GA_PrimitiveGroup > GA_PrimitiveGroupUPtr
#define GU_API
Definition: GU_API.h:14
GA_Offset getInputVertex() const
Definition: GU_Flatten.h:96
GLuint const GLchar * name
Definition: glcorearb.h:786
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
GLdouble t
Definition: glad.h:2397
void setU(fpreal u)
Definition: GU_Flatten.h:98
fpreal64 fpreal
Definition: SYS_Types.h:277
GU_API Status flattenIsland(Method method, const Island &island, const ConstraintSet &constraints, RWHandleV3R uvh)
fpreal v() const
Definition: GU_Flatten.h:101
GLubyte GLubyte GLubyte GLubyte w
Definition: glcorearb.h:857
ImageBuf OIIO_API add(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
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:102
void setToRemove(bool b)
Definition: GU_Flatten.h:103
UT_StringArray JOINTS hip
UT_UniquePtr< GA_EdgeGroup > GA_EdgeGroupUPtr
Definition: GA_EdgeGroup.h:466
fpreal u() const
Definition: GU_Flatten.h:100
void setV(fpreal v)
Definition: GU_Flatten.h:99