HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SOP_HOMWave.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  * This SOP provides an example of how to access HOM from C++.
27  *
28  * NOTE:
29  * See SOP_HOMWave.py for a pure Python implementation of this SOP.
30  */
31 
32 /// This SOP provides an example of how to access HOM from C++. See
33 /// SOP_HOMWave.py and related files for a pure Python implementation of this
34 /// SOP.
35 /// @see @ref HOM/SOP_HOMWave.py, @ref HOM/SOP_HOMWaveNumpy.py, @ref HOM/SOP_HOMWaveInlinecpp.py, @ref SOP/SOP_CPPWave.C, @ref SOP/SOP_VEXWave.vfl
36 
37 #include "SOP_HOMWave.h"
38 #include <OP/OP_OperatorTable.h>
39 #include <HOM/HOM_Module.h>
40 #include <HOM/HOM_SopNode.h>
41 #include <HOM/HOM_Geometry.h>
42 #include <HOM/HOM_Vector3.h>
43 #include <HOM/HOM_Point.h>
44 #include <HOM/HOM_NodeType.h>
45 #include <UT/UT_DSOVersion.h>
46 #include <UT/UT_UniquePtr.h>
47 
48 using namespace HDK_Sample;
49 
50 void
52 {
53  table->addOperator(new OP_Operator(
54  "hom_wave", "HOM Wave",
56  /*min_num_inputs=*/1, /*max_num_inputs=*/1));
57 }
58 
59 OP_Node *
61 {
62  return new SOP_HOMWave(net, name, op);
63 }
64 
65 // List any of the sop's parameters here:
68  PRM_Template(),
69 };
70 
71 SOP_HOMWave::SOP_HOMWave(
72  OP_Network *network, const char *name, OP_Operator *op_type)
73  : SOP_HOM(network, name, op_type)
74 {
75 }
76 
77 void
78 SOP_HOMWave::cookWithHOM()
79 {
80  HOM_Module &hou = HOM();
81 
82  // This C++ code does the equivalent of this Python code:
83  //
84  // import math
85  // geo = hou.pwd().geometry()
86  // f = hou.frame() * 0.03
87  // for p in geo.points():
88  // pos = p.position()
89  // pos[1] = math.sin(pos[0] * 0.2 + pos[2] * 0.3 + f)
90  // p.setPosition(pos)
91  //
92  // It modifies the input geometry's y coordinate to create an animated
93  // sin wave. Connect this sop to the output of a grid and press play.
94 
96  dynamic_cast<HOM_SopNode*>(hou.pwd()))->geometry());
97 
98  double f = hou.frame() * 0.03;
99  std::vector<HOM_ElemPtr<HOM_Point> > points = geo->points();
100  std::vector<double> pos_vector(3);
101  for (int i=0; i<points.size(); ++i)
102  {
103  UT_UniquePtr<HOM_Point> p(points[i].myPointer);
104 
105  UT_UniquePtr<HOM_Vector3> pos_ptr(p->position());
106  HOM_Vector3 &pos = *pos_ptr;
107  pos[1] = sin(pos[0] * 0.2 + pos[2] * 0.3 + f);
108 
109  for (int i=0; i<3; ++i)
110  pos_vector[i] = pos[i];
111  p->setPosition(pos_vector);
112  }
113 }
114 
GLdouble GLdouble GLint GLint const GLdouble * points
Definition: glad.h:2676
static OP_Node * myConstructor(OP_Network *network, const char *name, OP_Operator *op_type)
Definition: SOP_HOMWave.C:60
UT_UniquePtr< T > HOMdel(T *hom_object)
Definition: HOM_Module.h:1165
bool addOperator(OP_Operator *op, std::ostream *err=nullptr)
void newSopOperator(OP_OperatorTable *table)
Definition: SOP_HOMWave.C:51
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
GLfloat f
Definition: glcorearb.h:1926
static PRM_Template myTemplateList[]
Definition: SOP_HOMWave.h:44
virtual double frame(bool full_precision=false)=0
virtual HOM_Node * pwd()=0
GLuint const GLchar * name
Definition: glcorearb.h:786
GLenum GLenum GLsizei void * table
Definition: glad.h:5129
HOM_API HOM_Module & HOM()
SYS_API double sin(double x)
Definition: SYS_FPUMath.h:71