HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CHOP_Stair.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 
28 #include <SYS/SYS_Math.h>
29 #include <UT/UT_DSOVersion.h>
30 #include <OP/OP_OperatorTable.h>
31 
32 #include <CH/CH_LocalVariable.h>
33 #include <PRM/PRM_Include.h>
34 #include <CHOP/PRM_ChopShared.h>
35 
36 #include <CHOP/CHOP_Handle.h>
37 #include <OP/OP_Channels.h>
38 #include "CHOP_Stair.h"
39 
40 using namespace HDK_Sample;
41 
42 CHOP_SWITCHER2(4, "Stair", 8, "Channel");
43 
44 // Names of gadgets
45 static PRM_Name names[] =
46 {
47  // Stair tab
48  PRM_Name("number", "Number of Stairs"),
49  PRM_Name("height", "Stair Height"),
50  PRM_Name("offset", "Offset"),
51  PRM_Name("direction", "Direction"),
52 
53  // Channel tab
54  PRM_Name("channelname", "Channel Name"),
55  // RangeName
56  // StartName,
57  // EndName,
58  // SampleRateName
59  // ExtendLeftName
60  // ExtendRightName
61  // DefaultValueName
62 };
63 
64 // Menus
65 static PRM_Name CHOPstairMenu[] = {
66  PRM_Name("up", "Up"),
67  PRM_Name("down", "Down"),
68  PRM_Name(0),
69 };
70 
71 static PRM_ChoiceList stairMenu(PRM_CHOICELIST_SINGLE,CHOPstairMenu);
72 
73 // Defaults
74 static PRM_Default nameDefault(0,"chan1");
75 static PRM_Range stairRange(PRM_RANGE_RESTRICTED, 0, PRM_RANGE_UI, 10);
76 
79 {
81 
82  // page 1
83  PRM_Template(PRM_INT_J, 1, &names[0], PRMoneDefaults, 0, &stairRange),
84  PRM_Template(PRM_FLT_J, 1, &names[1], PRMoneDefaults, 0,
86  PRM_Template(PRM_FLT_J, 1, &names[2], PRMzeroDefaults, 0,
88  PRM_Template(PRM_ORD, 1, &names[3], PRMzeroDefaults, &stairMenu),
89 
90  // page 2
91  PRM_Template(PRM_STRING, 1, &names[4], &nameDefault,0),
104 
105  PRM_Template(),
106 };
109 
110 
111 
112 enum
113 {
114  VAR_C = 200,
115  VAR_NC = 201
116 };
117 
120  { "C", VAR_C, 0 },
121  { "NC", VAR_NC, 0 },
122  { 0, 0, 0 }
123 };
126 
127 bool
129 {
130  switch( index )
131  {
132  case VAR_C:
133  v = (fpreal) my_C;
134  return true;
135 
136  case VAR_NC:
137  v = (fpreal) my_NC;
138  return true;
139  }
140 
141  return CHOP_Node::evalVariableValue(v, index, thread);
142 }
143 
144 OP_Node *
146  const char *name,
147  OP_Operator *op)
148 {
149  return new CHOP_Stair(net, name, op);
150 }
151 
153  const char *name,
154  OP_Operator *op)
155  : CHOP_Node(net, name, op),
156  myExpandArray()
157 {
158  myParmBase = getParmList()->getParmIndex( names[0].getToken() );
159  my_C = 0;
160  my_NC = 0;
161 }
162 
164 {
165 }
166 
167 bool
169 {
170  bool changes = CHOP_Node::updateParmsFlags();
171 
172  bool use_startend = (RANGE(0.0) == RANGE_USER_ENTERED);
173  changes |= enableParm("start", use_startend);
174  changes |= enableParm("end", use_startend);
175 
176  if(LEXTEND() == CL_TRACK_DEFAULT || REXTEND() == CL_TRACK_DEFAULT)
177  changes |= enableParm(CHOP_DefaultValueName.getToken(),1);
178  else
179  changes |= enableParm(CHOP_DefaultValueName.getToken(),0);
180 
181  return changes;
182 }
183 
184 
185 #define START_HANDLE 1
186 #define END_HANDLE 2
187 #define OFFSET_HANDLE 3
188 
189 void
191 {
193  CHOP_HandleLook hlook;
194  fpreal start, end;
196 
197  destroyHandles();
198 
199  // Grab the values for our handles
201  xoffset = (end - start) * 0.75 + start;
202  yoffset = OFFSET(context.getTime());
203 
204  if (RANGE(context.getTime()) == RANGE_USER_ENTERED)
205  {
206  // If the parameter has a channel, use a guide rather than a handle.
207  hlook = myChannels->getChannel("start") ? HANDLE_GUIDE : HANDLE_BOX;
208 
209  // This creates a new handle for the start of the clip, and appends it
210  // to the list of handles. It will have the label 'start' to the
211  // bottom-right of the handle, and appear in bar view mode as well. It
212  // moves horizontally along the frame axis.
213  handle = new CHOP_Handle(this, "start", START_HANDLE, start, 0.0,
214  hlook, HANDLE_HORIZONTAL, (HANDLE_BAR |
216  myHandles.append(handle);
217 
218  // The end of the clip also has a handle for adjusting the clip. Other
219  // than the position, it is similar to the start handle.
220  hlook = myChannels->getChannel("end") ? HANDLE_GUIDE : HANDLE_BOX;
221  handle = new CHOP_Handle(this, "end", END_HANDLE, end, 0.0,
222  hlook, HANDLE_HORIZONTAL, (HANDLE_BAR |
225  myHandles.append(handle);
226  }
227 
228  // This is a horizontal handle which moves along the value axis to adjust
229  // the height of a stair. This handle only appears in graph mode.
230  hlook = myChannels->getChannel("offset") ? HANDLE_GUIDE : HANDLE_BOX;
231  handle = new CHOP_Handle(this, "offset", OFFSET_HANDLE, xoffset, yoffset,
232  hlook, HANDLE_VERTICAL,
234  myHandles.append(handle);
235 }
236 
237 fpreal
239 {
240  fpreal result = 0.0;
241  OP_Network *net = 0;
242  fpreal offset;
243 
244  if (hdata->shift)
245  {
246  setCurrent(1);
247  if ((net = getParent()))
248  net->pickRequest(this, 0);
249  }
250 
251  switch(handle->getId())
252  {
253  case START_HANDLE:
254  offset = CL_RINT(hdata->xoffset);
255  if (!hdata->shift)
256  SET_START(myHandleCookTime, offset);
257  result = toUnit(offset, hdata->unit);
258  break;
259 
260  case END_HANDLE:
261  offset = CL_RINT(hdata->xoffset);
262  if (!hdata->shift)
263  SET_END(myHandleCookTime, offset);
264  result = toUnit(offset, hdata->unit, 1);
265  break;
266 
267  case OFFSET_HANDLE:
268  if (!hdata->shift)
269  SET_OFFSET(myHandleCookTime, hdata->yoffset);
270  result = hdata->yoffset;
271  break;
272  }
273 
274  return result;
275 }
276 
277 
278 fpreal
280 {
281  SET_START(t, new_offset);
282  return new_offset;
283 }
284 
285 OP_ERROR
287 {
288  CL_Track *track;
289  fpreal samplerate;
290  fpreal start, end;
291  int left, right;
292  fpreal *data;
293  UT_String name;
294  fpreal value;
295  fpreal defvalue;
296  int nchan;
297  fpreal stepheight;
298  fpreal index, idxstep;
299  int i;
300 
301  destroyClip();
302  samplerate = RATE(context.getTime());
303  if(SYSequalZero(samplerate))
304  {
306  return error();
307  }
308  myClip->setSampleRate(samplerate);
309 
310  // Read non-expression parms
311 
312  CHAN_NAME(name, context.getTime());
313  nchan = myExpandArray.setPattern(name);
314 
315  my_NC = nchan;
316 
318 
319  left = LEXTEND();
320  right = REXTEND();
321 
322  myClip->setTrackLength((int)(end-start+1));
323  myClip->setStart(start);
324 
325  // Create the tracks
326 
327  for (my_C=0; my_C < nchan; my_C++)
328  {
329  defvalue = DEFAULT(context.getTime());
330  value = OFFSET(context.getTime()); // reread for local vars
331  idxstep = (end-start+1) / (NUMBER(context.getTime()) + 1);
332  stepheight = HEIGHT(context.getTime());
333  if (DIRECTION() == 1)
334  stepheight *= -1;
335 
336  track = myClip->addTrack(myExpandArray(my_C));
337 
338  track->setLeft((CL_TrackOutside)left);
339  track->setRight((CL_TrackOutside)right);
340  if(left == CL_TRACK_DEFAULT || right == CL_TRACK_DEFAULT)
341  track->setDefault(defvalue);
342 
343  data = track->getData();
344  track->constant(0);
345  index = idxstep;
346 
347  for(i=0; i < (end-start+1); i++)
348  {
349  if (i && i > index)
350  {
351  value += stepheight;
352  index += idxstep;
353  }
354 
355  data[i] = value;
356  }
357  }
358 
359  my_C = 0;
360 
361  return error();
362 }
363 
364 //----------------------------------------------------------------------------
365 
366 void
368  {
369  table->addOperator(
370  new OP_Operator("hdk_stair", // Internal name
371  "HDK Stair", // UI name
372  CHOP_Stair::myConstructor, // CHOP constructor
373  &CHOP_Stair::myTemplatePair, // Parameters
374  0, // Min # of inputs
375  0, // Max # of inputs
376  &CHOP_Stair::myVariablePair, // Local variables
377  OP_FLAG_GENERATOR) // generator/source
378  );
379 }
CL_Track * addTrack(const UT_StringHolder &name)
CHOP_API PRM_Name CHOP_RangeName
void cookMyHandles(OP_Context &context) override
Places the handles along the channels.
Definition: CHOP_Stair.C:190
PRM_API PRM_Default PRMtwoDefaults[]
CL_Clip * myClip
Definition: CHOP_Node.h:588
GLint GLint GLint yoffset
Definition: glcorearb.h:412
CHOP_API PRM_Name CHOP_DefaultValueName
virtual OP_ERROR error()
PRM_API const PRM_Type PRM_FLT
static CH_LocalVariable myVariableList[]
Definition: CHOP_Stair.h:60
PRM_API const PRM_Type PRM_STRING
static OP_TemplatePair myTemplatePair
Definition: CHOP_Stair.h:57
static OP_VariablePair myVariablePair
Definition: CHOP_Stair.h:58
void constant(fpreal value=0)
GLint left
Definition: glcorearb.h:2005
GLboolean * data
Definition: glcorearb.h:131
const GLdouble * v
Definition: glcorearb.h:837
fpreal getTime() const
Definition: OP_Context.h:62
static OP_TemplatePair myTemplatePair
Definition: CHOP_Node.h:162
GLuint start
Definition: glcorearb.h:475
GLsizei const GLfloat * value
Definition: glcorearb.h:824
OP_ERROR cookMyChop(OP_Context &context) override
Overridden to generate our channel data.
Definition: CHOP_Stair.C:286
CHOP_API PRM_Default CHOP_SampleRateDefault
#define HANDLE_LABEL_BOTTOM
Definition: CHOP_Handle.h:41
#define END_HANDLE
Definition: CHOP_Stair.C:186
GLdouble right
Definition: glad.h:2817
PRM_API const PRM_Type PRM_ORD
bool setCurrent(bool on_off)
#define OFFSET_HANDLE
Definition: CHOP_Stair.C:187
#define OP_FLAG_GENERATOR
Definition: OP_Operator.h:82
fpreal myHandleCookTime
Definition: CHOP_Node.h:590
UT_ErrorSeverity
Definition: UT_Error.h:25
CHOP_API PRM_Range CHOP_FrameRange
CHOP_API PRM_Default CHOP_StartDefault
**But if you need a result
Definition: thread.h:613
bool addOperator(OP_Operator *op, std::ostream *err=nullptr)
static OP_VariablePair myVariablePair
Definition: CHOP_Node.h:163
void getParmIntervalInSamples(fpreal t, fpreal &start, fpreal &end, const char *range_parm=0, const char *start_parm=0, const char *end_parm=0)
PRM_API const PRM_Type PRM_INT_J
bool updateParmsFlags() override
short myParmBase
Definition: CHOP_Node.h:585
void setTrackLength(int length, int clear_extra=0)
CHOP_API PRM_ChoiceList CHOP_RangeMenu
bool evalVariableValue(fpreal &v, int index, int thread) override
Stair has some local variables defined, this returns their value.
Definition: CHOP_Stair.C:128
fpreal shiftStart(fpreal new_offset, fpreal t) override
Definition: CHOP_Stair.C:279
CHOP_API PRM_Name CHOP_EndName
#define CL_RINT(x)
Definition: CL_Defines.h:25
GLintptr offset
Definition: glcorearb.h:665
fpreal handleChanged(CHOP_Handle *handle, CHOP_HandleData *hdata) override
Responds to user changes in any handle.
Definition: CHOP_Stair.C:238
CHOP_API PRM_Range CHOP_DefaultValueRange
bool enableParm(int pi, int state, int v=-1)
GLuint GLuint end
Definition: glcorearb.h:475
CHOP_SWITCHER2(4,"Stair", 8,"Channel")
CHOP_API PRM_Name CHOP_ExtendLeftName
CHOP_HandleList myHandles
Definition: CHOP_Node.h:589
OP_Channels * myChannels
GLint GLint xoffset
Definition: glcorearb.h:411
CHOP_API PRM_Name CHOP_ExtendRightName
GLuint const GLchar * name
Definition: glcorearb.h:786
void newChopOperator(OP_OperatorTable *table)
Definition: CHOP_Stair.C:367
unsigned int getId() const
Definition: CHOP_Handle.h:91
CHOP_HandleLook
Definition: CHOP_Handle.h:23
PRM_API const PRM_Type PRM_FLT_J
bool updateParmsFlags() override
Definition: CHOP_Stair.C:168
GLenum GLenum GLsizei void * table
Definition: glad.h:5129
PRM_API const PRM_Type PRM_SWITCHER
void setStart(fpreal start)
Definition: CL_Clip.h:73
exint append()
Definition: UT_Array.h:142
GLdouble t
Definition: glad.h:2397
~CHOP_Stair() override
Definition: CHOP_Stair.C:163
void destroyHandles()
bool SYSequalZero(const UT_Vector3T< T > &v)
Definition: UT_Vector3.h:1069
#define START_HANDLE
Definition: CHOP_Stair.C:185
CL_TrackOutside
Definition: CL_Track.h:36
PRM_API PRM_Default PRMoneDefaults[]
PRM_API PRM_Name PRMswitcherName
**Note that the tasks the is the thread number *for the or if it s being executed by a non pool thread(this *can happen in cases where the whole pool is occupied and the calling *thread contributes to running the work load).**Thread pool.Have fun
void setSampleRate(fpreal rate)
Definition: CL_Clip.h:87
fpreal64 fpreal
Definition: SYS_Types.h:277
static OP_Node * myConstructor(OP_Network *, const char *, OP_Operator *)
Definition: CHOP_Stair.C:145
GLuint index
Definition: glcorearb.h:786
CHOP_API PRM_Name CHOP_StartName
#define HANDLE_WIDTH_END
Definition: CHOP_Handle.h:46
fpreal * getData()
Definition: CL_Track.h:89
static void addError(int code, const char *msg=0)
Definition: CHOP_Node.h:256
virtual bool evalVariableValue(UT_String &val, int index, int thread)
int setPattern(const char *pattern, int *dirty_flag=0)
CHOP_Stair(OP_Network *net, const char *name, OP_Operator *op)
Definition: CHOP_Stair.C:152
void pickRequest(OP_NetworkBoxItem *item, int shift)
OP_Network * getParent() const
Definition: OP_Node.h:505
CHOP_API PRM_Name CHOP_SampleRateName
CHOP_API PRM_ChoiceList CHOP_ExtendMenu
void destroyClip()
#define HANDLE_BAR
Definition: CHOP_Handle.h:43
int getParmIndex(const PRM_Parm *parm) const
#define HANDLE_LABEL_RIGHT
Definition: CHOP_Handle.h:38
void setDefault(fpreal value)
const CH_Channel * getChannel(const UT_StringRef &name) const
PRM_API PRM_Default PRMzeroDefaults[]
SYS_FORCE_INLINE PRM_ParmList * getParmList()
Definition: PRM_ParmOwner.h:71
CHOP_API PRM_Range CHOP_SampleRateRange
static PRM_Template myTemplateList[]
Definition: CHOP_Stair.h:59
CHOP_API PRM_Default CHOP_EndDefault
fpreal toUnit(fpreal index, CL_Unit unit=UNIT_NONE, int end_width=0) const
void setRight(CL_TrackOutside type)
void setLeft(CL_TrackOutside type)
const char * getToken() const
Definition: PRM_Name.h:79