HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CHOP_Stair.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 
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 
72  | PRM_CHOICELIST_REPLACE),CHOPstairMenu);
73 
74 // Defaults
75 static PRM_Default nameDefault(0,"chan1");
76 static PRM_Range stairRange(PRM_RANGE_RESTRICTED, 0, PRM_RANGE_UI, 10);
77 
80 {
82 
83  // page 1
84  PRM_Template(PRM_INT_J, 1, &names[0], PRMoneDefaults, 0, &stairRange),
85  PRM_Template(PRM_FLT_J, 1, &names[1], PRMoneDefaults, 0,
87  PRM_Template(PRM_FLT_J, 1, &names[2], PRMzeroDefaults, 0,
89  PRM_Template(PRM_ORD, 1, &names[3], PRMzeroDefaults, &stairMenu),
90 
91  // page 2
92  PRM_Template(PRM_STRING, 1, &names[4], &nameDefault,0),
105 
106  PRM_Template(),
107 };
110 
111 
112 
113 enum
114 {
115  VAR_C = 200,
116  VAR_NC = 201
117 };
118 
121  { "C", VAR_C, 0 },
122  { "NC", VAR_NC, 0 },
123  { 0, 0, 0 }
124 };
127 
128 bool
130 {
131  switch( index )
132  {
133  case VAR_C:
134  v = (fpreal) my_C;
135  return true;
136 
137  case VAR_NC:
138  v = (fpreal) my_NC;
139  return true;
140  }
141 
142  return CHOP_Node::evalVariableValue(v, index, thread);
143 }
144 
145 OP_Node *
147  const char *name,
148  OP_Operator *op)
149 {
150  return new CHOP_Stair(net, name, op);
151 }
152 
154  const char *name,
155  OP_Operator *op)
156  : CHOP_Node(net, name, op),
157  myExpandArray()
158 {
159  myParmBase = getParmList()->getParmIndex( names[0].getToken() );
160  my_C = 0;
161  my_NC = 0;
162 }
163 
165 {
166 }
167 
168 bool
170 {
171  bool changes = CHOP_Node::updateParmsFlags();
172 
173  bool use_startend = (RANGE(0.0) == RANGE_USER_ENTERED);
174  changes |= enableParm("start", use_startend);
175  changes |= enableParm("end", use_startend);
176 
177  if(LEXTEND() == CL_TRACK_DEFAULT || REXTEND() == CL_TRACK_DEFAULT)
178  changes |= enableParm(CHOP_DefaultValueName.getToken(),1);
179  else
180  changes |= enableParm(CHOP_DefaultValueName.getToken(),0);
181 
182  return changes;
183 }
184 
185 
186 #define START_HANDLE 1
187 #define END_HANDLE 2
188 #define OFFSET_HANDLE 3
189 
190 void
192 {
193  CHOP_Handle *handle;
194  CHOP_HandleLook hlook;
195  fpreal start, end;
197 
198  destroyHandles();
199 
200  // Grab the values for our handles
202  xoffset = (end - start) * 0.75 + start;
203  yoffset = OFFSET(context.getTime());
204 
205  if (RANGE(context.getTime()) == RANGE_USER_ENTERED)
206  {
207  // If the parameter has a channel, use a guide rather than a handle.
208  hlook = myChannels->getChannel("start") ? HANDLE_GUIDE : HANDLE_BOX;
209 
210  // This creates a new handle for the start of the clip, and appends it
211  // to the list of handles. It will have the label 'start' to the
212  // bottom-right of the handle, and appear in bar view mode as well. It
213  // moves horizontally along the frame axis.
214  handle = new CHOP_Handle(this, "start", START_HANDLE, start, 0.0,
215  hlook, HANDLE_HORIZONTAL, (HANDLE_BAR |
217  myHandles.append(handle);
218 
219  // The end of the clip also has a handle for adjusting the clip. Other
220  // than the position, it is similar to the start handle.
221  hlook = myChannels->getChannel("end") ? HANDLE_GUIDE : HANDLE_BOX;
222  handle = new CHOP_Handle(this, "end", END_HANDLE, end, 0.0,
223  hlook, HANDLE_HORIZONTAL, (HANDLE_BAR |
226  myHandles.append(handle);
227  }
228 
229  // This is a horizontal handle which moves along the value axis to adjust
230  // the height of a stair. This handle only appears in graph mode.
231  hlook = myChannels->getChannel("offset") ? HANDLE_GUIDE : HANDLE_BOX;
232  handle = new CHOP_Handle(this, "offset", OFFSET_HANDLE, xoffset, yoffset,
233  hlook, HANDLE_VERTICAL,
235  myHandles.append(handle);
236 }
237 
238 fpreal
240 {
241  fpreal result = 0.0;
242  OP_Network *net = 0;
243  fpreal offset;
244 
245  if (hdata->shift)
246  {
247  setCurrent(1);
248  if ((net = getParent()))
249  net->pickRequest(this, 0);
250  }
251 
252  switch(handle->getId())
253  {
254  case START_HANDLE:
255  offset = CL_RINT(hdata->xoffset);
256  if (!hdata->shift)
257  SET_START(myHandleCookTime, offset);
258  result = toUnit(offset, hdata->unit);
259  break;
260 
261  case END_HANDLE:
262  offset = CL_RINT(hdata->xoffset);
263  if (!hdata->shift)
264  SET_END(myHandleCookTime, offset);
265  result = toUnit(offset, hdata->unit, 1);
266  break;
267 
268  case OFFSET_HANDLE:
269  if (!hdata->shift)
270  SET_OFFSET(myHandleCookTime, hdata->yoffset);
271  result = hdata->yoffset;
272  break;
273  }
274 
275  return result;
276 }
277 
278 
279 fpreal
281 {
282  SET_START(t, new_offset);
283  return new_offset;
284 }
285 
286 OP_ERROR
288 {
289  CL_Track *track;
290  fpreal samplerate;
291  fpreal start, end;
292  int left, right;
293  fpreal *data;
294  UT_String name;
295  fpreal value;
296  fpreal defvalue;
297  int nchan;
298  fpreal stepheight;
299  fpreal index, idxstep;
300  int i;
301 
302  destroyClip();
303  samplerate = RATE(context.getTime());
304  if(SYSequalZero(samplerate))
305  {
307  return error();
308  }
309  myClip->setSampleRate(samplerate);
310 
311  // Read non-expression parms
312 
313  CHAN_NAME(name, context.getTime());
314  nchan = myExpandArray.setPattern(name);
315 
316  my_NC = nchan;
317 
319 
320  left = LEXTEND();
321  right = REXTEND();
322 
323  myClip->setTrackLength((int)(end-start+1));
324  myClip->setStart(start);
325 
326  // Create the tracks
327 
328  for (my_C=0; my_C < nchan; my_C++)
329  {
330  defvalue = DEFAULT(context.getTime());
331  value = OFFSET(context.getTime()); // reread for local vars
332  idxstep = (end-start+1) / (NUMBER(context.getTime()) + 1);
333  stepheight = HEIGHT(context.getTime());
334  if (DIRECTION() == 1)
335  stepheight *= -1;
336 
337  track = myClip->addTrack(myExpandArray(my_C));
338 
339  track->setLeft((CL_TrackOutside)left);
340  track->setRight((CL_TrackOutside)right);
341  if(left == CL_TRACK_DEFAULT || right == CL_TRACK_DEFAULT)
342  track->setDefault(defvalue);
343 
344  data = track->getData();
345  track->constant(0);
346  index = idxstep;
347 
348  for(i=0; i < (end-start+1); i++)
349  {
350  if (i && i > index)
351  {
352  value += stepheight;
353  index += idxstep;
354  }
355 
356  data[i] = value;
357  }
358  }
359 
360  my_C = 0;
361 
362  return error();
363 }
364 
365 //----------------------------------------------------------------------------
366 
367 void
369  {
370  table->addOperator(
371  new OP_Operator("hdk_stair", // Internal name
372  "HDK Stair", // UI name
373  CHOP_Stair::myConstructor, // CHOP constructor
374  &CHOP_Stair::myTemplatePair, // Parameters
375  0, // Min # of inputs
376  0, // Max # of inputs
377  &CHOP_Stair::myVariablePair, // Local variables
378  OP_FLAG_GENERATOR) // generator/source
379  );
380 }
CHOP_API PRM_Name CHOP_RangeName
PRM_API PRM_Default PRMtwoDefaults[]
CL_Clip * myClip
Definition: CHOP_Node.h:566
GLint GLint GLint yoffset
Definition: glcorearb.h:411
int setCurrent(int on_off)
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:2004
fpreal getTime() const
Definition: OP_Context.h:34
virtual fpreal handleChanged(CHOP_Handle *handle, CHOP_HandleData *hdata)
Responds to user changes in any handle.
Definition: CHOP_Stair.C:239
const GLdouble * v
Definition: glcorearb.h:836
static OP_TemplatePair myTemplatePair
Definition: CHOP_Node.h:157
GLuint start
Definition: glcorearb.h:474
CHOP_API PRM_Default CHOP_SampleRateDefault
virtual bool updateParmsFlags()
Definition: CHOP_Stair.C:169
#define HANDLE_LABEL_BOTTOM
Definition: CHOP_Handle.h:41
#define END_HANDLE
Definition: CHOP_Stair.C:187
PRM_API const PRM_Type PRM_ORD
CL_Track * addTrack(const char *name)
#define OFFSET_HANDLE
Definition: CHOP_Stair.C:188
#define OP_FLAG_GENERATOR
Definition: OP_Operator.h:67
fpreal myHandleCookTime
Definition: CHOP_Node.h:568
UT_ErrorSeverity
Definition: UT_Error.h:25
CHOP_API PRM_Range CHOP_FrameRange
CHOP_API PRM_Default CHOP_StartDefault
PRM_ChoiceListType
bool addOperator(OP_Operator *op, std::ostream *err=nullptr)
static OP_VariablePair myVariablePair
Definition: CHOP_Node.h:158
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
short myParmBase
Definition: CHOP_Node.h:563
png_uint_32 i
Definition: png.h:2877
void setTrackLength(int length, int clear_extra=0)
CHOP_API PRM_ChoiceList CHOP_RangeMenu
PRM_ParmList * getParmList()
Definition: PRM_ParmOwner.h:65
CHOP_API PRM_Name CHOP_EndName
#define CL_RINT(x)
Definition: CL_Defines.h:25
CHOP_API PRM_Range CHOP_DefaultValueRange
bool enableParm(int pi, int state, int v=-1)
GLuint GLuint end
Definition: glcorearb.h:474
CHOP_SWITCHER2(4,"Stair", 8,"Channel")
GLintptr offset
Definition: glcorearb.h:664
CHOP_API PRM_Name CHOP_ExtendLeftName
CHOP_HandleList myHandles
Definition: CHOP_Node.h:567
OP_Channels * myChannels
GLint GLint xoffset
Definition: glcorearb.h:410
CHOP_API PRM_Name CHOP_ExtendRightName
GLboolean * data
Definition: glcorearb.h:130
void newChopOperator(OP_OperatorTable *table)
Definition: CHOP_Stair.C:368
GLuint const GLchar * name
Definition: glcorearb.h:785
unsigned int getId() const
Definition: CHOP_Handle.h:91
void destroyClip(void)
CHOP_HandleLook
Definition: CHOP_Handle.h:23
PRM_API const PRM_Type PRM_FLT_J
PRM_API const PRM_Type PRM_SWITCHER
const CH_Channel * getChannel(const char *name) const
void setStart(fpreal start)
Definition: CL_Clip.h:73
virtual fpreal shiftStart(fpreal new_offset, fpreal t)
Definition: CHOP_Stair.C:280
#define START_HANDLE
Definition: CHOP_Stair.C:186
CL_TrackOutside
Definition: CL_Track.h:37
PRM_API PRM_Default PRMoneDefaults[]
PRM_API PRM_Name PRMswitcherName
GLsizei const GLfloat * value
Definition: glcorearb.h:823
double fpreal
Definition: SYS_Types.h:263
void setSampleRate(fpreal rate)
Definition: CL_Clip.h:87
static OP_Node * myConstructor(OP_Network *, const char *, OP_Operator *)
Definition: CHOP_Stair.C:146
GLuint index
Definition: glcorearb.h:785
CHOP_API PRM_Name CHOP_StartName
#define HANDLE_WIDTH_END
Definition: CHOP_Handle.h:46
fpreal * getData()
Definition: CL_Track.h:84
exint append(void)
Definition: UT_Array.h:95
static void addError(int code, const char *msg=0)
Definition: CHOP_Node.h:250
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:153
void pickRequest(OP_NetworkBoxItem *item, int shift)
OP_Network * getParent() const
Definition: OP_Node.h:513
CHOP_API PRM_Name CHOP_SampleRateName
CHOP_API PRM_ChoiceList CHOP_ExtendMenu
void destroyHandles(void)
OP_ERROR cookMyChop(OP_Context &context)
Overridden to generate our channel data.
Definition: CHOP_Stair.C:287
#define HANDLE_BAR
Definition: CHOP_Handle.h:43
virtual bool evalVariableValue(fpreal &v, int index, int thread)
Stair has some local variables defined, this returns their value.
Definition: CHOP_Stair.C:129
int getParmIndex(const PRM_Parm *parm) const
#define HANDLE_LABEL_RIGHT
Definition: CHOP_Handle.h:38
void setDefault(fpreal value)
PRM_API PRM_Default PRMzeroDefaults[]
CHOP_API PRM_Range CHOP_SampleRateRange
void cookMyHandles(OP_Context &context)
Places the handles along the channels.
Definition: CHOP_Stair.C:191
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)
virtual bool updateParmsFlags()
const char * getToken() const
Definition: PRM_Name.h:40