HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OBJ_WorldAlign.C
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2024
3  * Side Effects Software Inc. All rights reserved.
4  *
5  * Redistribution and use of Houdini Development Kit samples in source and
6  * binary forms, with or without modification, are permitted provided that the
7  * following conditions are met:
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. The name of Side Effects Software may not be used to endorse or
11  * promote products derived from this software without specific prior
12  * written permission.
13  *
14  * THIS SOFTWARE IS PROVIDED BY SIDE EFFECTS SOFTWARE `AS IS' AND ANY EXPRESS
15  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
17  * NO EVENT SHALL SIDE EFFECTS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
19  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
20  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
23  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  *----------------------------------------------------------------------------
26  *
27  * This class implements an object that aligns the inherited orientation to the
28  * world axes while preserving the location in space. It also illustrates how to
29  * hide all the parameters inherited from the base class.
30  *
31  */
32 
33 #include "OBJ_WorldAlign.h"
34 #include <UT/UT_DSOVersion.h>
35 #include <UT/UT_DMatrix3.h>
36 #include <OP/OP_OperatorPair.h>
37 #include <OP/OP_OperatorTable.h>
38 
39 
40 using namespace HDK_Sample;
41 
42 
43 
44 // Constructor for new object class
46  const char *name, OP_Operator *op )
47  : OBJ_Geometry(net, name, op)
48 {
49  // initialize local member data here.
50 }
51 
52 
53 
54 // virtual destructor for new object class
56 {
57 }
58 
59 
60 
61 // this is the template list that defines the new parameters that are
62 // used by the worldalign object. In this particular case the list is empty
63 // because we have no new parameters.
64 static PRM_Template templatelist[] =
65 {
66  // blank terminating Template.
67  PRM_Template()
68 };
69 
70 
71 
72 // this function copies the original parameter and then adds an invisible flag
73 static void
74 copyParmWithInvisible(PRM_Template &src, PRM_Template &dest)
75 {
76  PRM_Name * new_name;
77 
78  new_name = new PRM_Name(src.getToken(), src.getLabel(),
79  src.getExpressionFlag());
80  new_name->harden();
81  dest.initialize(
83  src.getTypeExtended(),
84  src.exportLevel(),
85  src.getVectorSize(),
86  new_name,
87  src.getFactoryDefaults(),
88  src.getChoiceListPtr(),
89  src.getRangePtr(),
90  src.getCallback(),
91  src.getSparePtr(),
92  src.getParmGroup(),
93  (const char *)src.getHelpText(),
94  src.getConditionalBasePtr());
95 }
96 
97 
98 
99 // this function returns the OP_TemplatePair that combines the parameters
100 // of this object with those of its ancestors in the (object class hierarchy)
103 {
104  OP_TemplatePair *align, *geo;
105 
106  // The parm templates here are not created as a static list because
107  // if that static list was built before the OBJbaseTemplate static list
108  // (which it references) then that list would be corrupt. Thus we have
109  // to force our static list to be created after OBJbaseTemplate.
110  static PRM_Template *theTemplate = 0;
111 
112  if(!theTemplate)
113  {
114  PRM_Template *obj_template;
115  int i;
116  int size;
117  UT_String parm_name;
118 
120  size = PRM_Template::countTemplates( obj_template );
121  theTemplate = new PRM_Template[size + 1]; // add +1 for sentinel
122  for( i = 0; i < size; i++ )
123  {
124  theTemplate[i] = obj_template[i];
125  theTemplate[i].getToken( parm_name );
126 
127  // leave only the translation parameter visible (and its containing
128  // switcher)
129  if( parm_name != "t" && parm_name != "stdswitcher" )
130  copyParmWithInvisible( obj_template[i], theTemplate[i] );
131  }
132  }
133 
134  // Here, we have to "inherit" template pairs from geometry and beyond. To
135  // do this, we first need to instantiate our template list, then add the
136  // base class templates.
137  align = new OP_TemplatePair(templatelist, prevstuff);
138  geo = new OP_TemplatePair(theTemplate, align);
139 
140  return geo;
141 }
142 
143 
144 
145 // the myConstructor method is used to create new objects of the correct
146 // type from the OperatorTable. This method is passed to the OP_Operator
147 // constructor and then is invoked whenever a new node needs to be created.
148 OP_Node *
150 {
151  return new OBJ_WorldAlign(net, name, op);
152 }
153 
154 
155 
156 OP_ERROR
158 {
159  OP_ERROR errorstatus;
161  UT_DMatrix4 rotation4;
162 
163  // OBJ_Geometry::cookMyObj computes the local and global transform, and
164  // dirties the inverse of the global transform matrix. These are stored
165  // in myXform, myWorldXform, and myIWorldXform, respectively.
166  errorstatus = OBJ_Geometry::cookMyObj(context);
167 
168  // get rid of the rotation component in the matrices
169  getWorldXform().extractRotate(rotation);
170  if( ! rotation.invert() )
171  {
172  rotation4 = rotation;
173  setWorldXform(rotation4 * getWorldXform());
174  setLocalXform(rotation4 * getLocalXform());
175  }
176 
177  return errorstatus;
178 }
179 
180 
181 
182 // this function installs the new object in houdini's object table.
183 // It is automatically called by Houdini when this dynamic library is loaded.
184 void
186 {
187  table->addOperator(new OP_Operator(
188  "hdk_worldalign", // operator unique name
189  "World Align", // operator label
190  OBJ_WorldAlign::myConstructor, // node instance constructor
191  OBJ_WorldAlign::buildTemplatePair(0), // parameters
193  0, // minimum inputs
194  1, // maximum inputs
195  nullptr)); // variables
196 }
PRM_Default * getFactoryDefaults() const
Definition: PRM_Template.h:313
void extractRotate(UT_Matrix3T< S > &dst) const
const UT_Matrix4D & getLocalXform() const
Definition: OBJ_Node.h:1440
const PRM_Type & getType() const
Definition: PRM_Template.h:206
void initialize(PRM_Type thetype, PRM_TypeExtended thetype_ext, PRM_Export theexportlevel, int thevectorsize, PRM_Name *thenameptr, PRM_Default *thedefaults, PRM_ChoiceList *thechoicelistptr, PRM_Range *therangeptr, PRM_Callback thecallbackfunc, PRM_SpareData *thespareptr, int theparmgroup, const char *thehelptext, PRM_ConditionalBase *thecondptr)
static OP_TemplatePair * buildTemplatePair(OP_TemplatePair *prevstuff)
static const char * theChildTableName
Definition: OBJ_Node.h:240
PRM_Callback getCallback() const
Definition: PRM_Template.h:264
PRM_SpareData * getSparePtr()
Definition: PRM_Template.h:217
UT_ErrorSeverity
Definition: UT_Error.h:25
bool addOperator(OP_Operator *op, std::ostream *err=nullptr)
static PRM_Template * getTemplateList(OBJ_ParmsStyle style)
const UT_StringHolder & getHelpText() const
Definition: PRM_Template.h:267
int getExpressionFlag() const
Definition: PRM_Template.h:440
const PRM_Range * getRangePtr() const
Definition: PRM_Template.h:262
OP_ERROR cookMyObj(OP_Context &context) override
SIM_API const UT_StringHolder rotation
virtual OP_ERROR cookMyObj(OP_Context &context)
void setLocalXform(const UT_Matrix4D &m)
Definition: OBJ_Node.h:1444
static OP_Node * myConstructor(OP_Network *net, const char *name, OP_Operator *entry)
GLuint const GLchar * name
Definition: glcorearb.h:786
OBJ_WorldAlign(OP_Network *net, const char *name, OP_Operator *op)
void getToken(UT_String &thestrref) const
Definition: PRM_Template.h:431
GLenum GLenum GLsizei void * table
Definition: glad.h:5129
const UT_Matrix4D & getWorldXform() const
Definition: OBJ_Node.h:1448
int getVectorSize() const
Definition: PRM_Template.h:212
PRM_TypeExtended getTypeExtended() const
Definition: PRM_Template.h:211
GLsizeiptr size
Definition: glcorearb.h:664
void setWorldXform(const UT_Matrix4D &m)
Definition: OBJ_Node.h:1453
void newObjectOperator(OP_OperatorTable *table)
PRM_Export exportLevel() const
Definition: PRM_Template.h:355
void harden()
Do a deep copy of its internal references.
Definition: PRM_Name.h:100
const PRM_ChoiceList * getChoiceListPtr() const
Definition: PRM_Template.h:235
PRM_API const PRM_Type PRM_TYPE_INVISIBLE
int getParmGroup() const
Definition: PRM_Template.h:359
PRM_ConditionalBase * getConditionalBasePtr()
Definition: PRM_Template.h:224
void getLabel(UT_String &thestrref) const
Definition: PRM_Template.h:435
static int countTemplates(const PRM_Template *templates, bool for_switcher=false, bool for_joins=false, bool for_rows=false)
GLenum src
Definition: glcorearb.h:1793