HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OBJ_WorldAlign.C
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018
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_OperatorTable.h>
37 
38 
39 using namespace HDK_Sample;
40 
41 
42 
43 // Constructor for new object class
45  const char *name, OP_Operator *op )
46  : OBJ_Geometry(net, name, op)
47 {
48  // initialize local member data here.
49 }
50 
51 
52 
53 // virtual destructor for new object class
55 {
56 }
57 
58 
59 
60 // this is the template list that defines the new parameters that are
61 // used by the worldalign object. In this particular case the list is empty
62 // because we have no new parameters.
63 static PRM_Template templatelist[] =
64 {
65  // blank terminating Template.
66  PRM_Template()
67 };
68 
69 
70 
71 // this function copies the original parameter and then adds an invisible flag
72 static void
73 copyParmWithInvisible(PRM_Template &src, PRM_Template &dest)
74 {
75  PRM_Name * new_name;
76 
77  new_name = new PRM_Name(src.getToken(), src.getLabel(),
78  src.getExpressionFlag());
79  new_name->harden();
80  dest.initialize(
82  src.getTypeExtended(),
83  src.exportLevel(),
84  src.getVectorSize(),
85  new_name,
86  src.getFactoryDefaults(),
87  src.getChoiceListPtr(),
88  src.getRangePtr(),
89  src.getCallback(),
90  src.getSparePtr(),
91  src.getParmGroup(),
92  (const char *)src.getHelpText(),
93  src.getConditionalBasePtr());
94 }
95 
96 
97 
98 // this function returns the OP_TemplatePair that combines the parameters
99 // of this object with those of its ancestors in the (object class hierarchy)
102 {
103  OP_TemplatePair *align, *geo;
104 
105  // The parm templates here are not created as a static list because
106  // if that static list was built before the OBJbaseTemplate static list
107  // (which it references) then that list would be corrupt. Thus we have
108  // to force our static list to be created after OBJbaseTemplate.
109  static PRM_Template *theTemplate = 0;
110 
111  if(!theTemplate)
112  {
113  PRM_Template *obj_template;
114  int i;
115  int size;
116  UT_String parm_name;
117 
119  size = PRM_Template::countTemplates( obj_template );
120  theTemplate = new PRM_Template[size + 1]; // add +1 for sentinel
121  for( i = 0; i < size; i++ )
122  {
123  theTemplate[i] = obj_template[i];
124  theTemplate[i].getToken( parm_name );
125 
126  // leave only the translation parameter visible (and its containing
127  // switcher)
128  if( parm_name != "t" && parm_name != "stdswitcher" )
129  copyParmWithInvisible( obj_template[i], theTemplate[i] );
130  }
131  }
132 
133  // Here, we have to "inherit" template pairs from geometry and beyond. To
134  // do this, we first need to instantiate our template list, then add the
135  // base class templates.
136  align = new OP_TemplatePair(templatelist, prevstuff);
137  geo = new OP_TemplatePair(theTemplate, align);
138 
139  return geo;
140 }
141 
142 
143 
144 // the myConstructor method is used to create new objects of the correct
145 // type from the OperatorTable. This method is passed to the OP_Operator
146 // constructor and then is invoked whenever a new node needs to be created.
147 OP_Node *
149 {
150  return new OBJ_WorldAlign(net, name, op);
151 }
152 
153 
154 
155 OP_ERROR
157 {
158  OP_ERROR errorstatus;
160  UT_DMatrix4 rotation4;
161 
162  // OBJ_Geometry::cookMyObj computes the local and global transform, and
163  // dirties the inverse of the global transform matrix. These are stored
164  // in myXform, myWorldXform, and myIWorldXform, respectively.
165  errorstatus = OBJ_Geometry::cookMyObj(context);
166 
167  // get rid of the rotation component in the matrices
168  getWorldXform().extractRotate(rotation);
169  if( ! rotation.invert() )
170  {
171  rotation4 = rotation;
172  setWorldXform(rotation4 * getWorldXform());
173  setLocalXform(rotation4 * getLocalXform());
174  }
175 
176  return errorstatus;
177 }
178 
179 
180 
181 // this function installs the new object in houdini's object table.
182 // It is automatically called by Houdini when this dynamic library is loaded.
183 void
185 {
186  table->addOperator(new OP_Operator(
187  "hdk_worldalign", // operator unique name
188  "World Align", // operator label
189  OBJ_WorldAlign::myConstructor, // node instance constructor
190  OBJ_WorldAlign::buildTemplatePair(0), // parameters
192  0, // minimum inputs
193  1, // maximum inputs
194  nullptr)); // variables
195 }
PRM_Default * getFactoryDefaults() const
Definition: PRM_Template.h:300
void extractRotate(UT_Matrix3T< S > &dst) const
const UT_Matrix4D & getLocalXform() const
Definition: OBJ_Node.h:1389
const PRM_Type & getType() const
Definition: PRM_Template.h:200
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:238
PRM_Callback getCallback() const
Definition: PRM_Template.h:258
PRM_SpareData * getSparePtr()
Definition: PRM_Template.h:211
UT_ErrorSeverity
Definition: UT_Error.h:25
bool addOperator(OP_Operator *op, std::ostream *err=nullptr)
png_uint_32 i
Definition: png.h:2877
const UT_String & getHelpText() const
Definition: PRM_Template.h:261
GLsizeiptr size
Definition: glcorearb.h:663
static PRM_Template * getTemplateList(OBJ_ParmsStyle style)
int invert()
Invert this matrix and return 0 if OK, 1 if singular.
int getExpressionFlag() const
Definition: PRM_Template.h:366
const PRM_Range * getRangePtr() const
Definition: PRM_Template.h:256
virtual OP_ERROR cookMyObj(OP_Context &context)
void setLocalXform(const UT_Matrix4D &m)
Definition: OBJ_Node.h:1393
static OP_Node * myConstructor(OP_Network *net, const char *name, OP_Operator *entry)
OBJ_WorldAlign(OP_Network *net, const char *name, OP_Operator *op)
GLuint const GLchar * name
Definition: glcorearb.h:785
void getToken(UT_String &thestrref) const
Definition: PRM_Template.h:357
const UT_Matrix4D & getWorldXform() const
Definition: OBJ_Node.h:1397
int getVectorSize() const
Definition: PRM_Template.h:206
PRM_TypeExtended getTypeExtended() const
Definition: PRM_Template.h:205
void setWorldXform(const UT_Matrix4D &m)
Definition: OBJ_Node.h:1402
void newObjectOperator(OP_OperatorTable *table)
PRM_Export exportLevel() const
Definition: PRM_Template.h:342
void harden()
virtual OP_ERROR cookMyObj(OP_Context &context)
const PRM_ChoiceList * getChoiceListPtr() const
Definition: PRM_Template.h:230
PRM_API const PRM_Type PRM_TYPE_INVISIBLE
int getParmGroup() const
Definition: PRM_Template.h:346
PRM_ConditionalBase * getConditionalBasePtr()
Definition: PRM_Template.h:221
void getLabel(UT_String &thestrref) const
Definition: PRM_Template.h:361
static int countTemplates(const PRM_Template *templates, bool for_switcher=false, bool for_joins=false, bool for_rows=false)
GLenum src
Definition: glcorearb.h:1792
MatType rotation(const Quat< typename MatType::value_type > &q, typename MatType::value_type eps=static_cast< typename MatType::value_type >(1.0e-8))
Return the rotation matrix specified by the given quaternion.
Definition: Mat.h:169