HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TS_cosKernel.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 /// @file TS_cosKernel.C
29 /// @brief Sample metaball kernel function
30 ///
31 /// This file demonstrates how to add a custom metaball kernel function that
32 /// will show up in all the Kernel Function parameter menus.
33 ///
34 /// Once this file is compiled, register this dso into a @c MetaKernels text
35 /// file that is found in your HOUDINI_PATH environment variable. For example:
36 /// - <tt>$HOME/Library/Preferences/houdini/X.Y/MetaKernels</tt> <b>(OSX)</b>
37 /// - <tt>$HOME/houdiniX.Y/MetaKernels</tt> <b>(other platforms)</b>
38 ///
39 /// The @c MetaKernels file should contain a list of .so/.dll paths that are
40 /// located relative to the search paths in the HOUDINI_DSO_PATH environment
41 /// variable. For example:
42 /// @code
43 /// # MetaKernels : Kernel extension table
44 /// TS_MyKernel.so
45 /// @endcode
46 /// In this case, you should install the TS_MyKernel.so file into the @c dso
47 /// subdirectory under where @c MetaKernels is found.
48 ///
49 #include <UT/UT_DSOVersion.h>
50 #include <TS/TS_Expression.h>
51 
52 #include <TS/TS_KernelList.h>
53 
54 static float
55 evalPosition(float t)
56 {
57  // Since distance passed in is squared, we must take the root...
58  t = SYSsqrt(t);
59  return SYScos(t*M_PI)*.5 + .5;
60 }
61 
62 static float
63 evalGradient(float t)
64 {
65  t = SYSsqrt(t);
66  return .5*SYSsin(t*M_PI);
67 }
68 
69 static float
70 cosFunc(float t)
71 {
72  if (t <= 0) return 1;
73  if (t >= 1) return 0;
74  return evalPosition(t);
75 }
76 
77 static UT_Interval
78 cosFuncRange(const UT_Interval &t)
79 {
80  if (t.max <= 0) return UT_Interval(1, 1);
81  if (t.min >= 1) return UT_Interval(0, 0);
82 
83  float min = cosFunc(t.max);
84  float max = cosFunc(t.min);
85  return (min > max) ? UT_Interval(max, min) : UT_Interval(min, max);
86 }
87 
88 static float
89 cosDFunc(float t)
90 {
91  if (t <= 0) return 0;
92  if (t >= 1) return 0;
93  return evalGradient(t);
94 }
95 
96 static UT_Interval
97 cosDFuncRange(const UT_Interval &t)
98 {
99  if (t.max <= 0 || t.min >= 1) return UT_Interval(0, 0);
100  float min = cosDFunc(t.min);
101  float max = cosDFunc(t.max);
102  return (min > max) ? UT_Interval(max, min) : UT_Interval(min, max);
103 }
104 
105 /// The kernel table definition
106 static TS_MetaKernel cosKernel = {
107  "cosine", // Token
108  "Half Cosine", // Label
109  cosFunc,
110  cosFuncRange,
111  cosDFunc,
112  cosDFuncRange
113 };
114 
115 /// Registration function which installs the kernel
116 void
118 {
119  TS_KernelList::getList()->addKernel(&cosKernel);
120 }
121 
void newMetaKernel()
Registration function which installs the kernel.
Definition: TS_cosKernel.C:117
#define M_PI
Definition: fmath.h:90
static TS_KernelList * getList()
ImageBuf OIIO_API min(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
GLdouble t
Definition: glad.h:2397
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
UT_IntervalT< float > UT_Interval
Definition: UT_Interval.h:135
void addKernel(TS_MetaKernel *kernel)