HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
VEX_Sort.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 example shows how array types can be accessed inside custom vex
27  * operators. The hdksort() function will sort an array object in-place.
28  * The hdkdecimate() function shows how the size of an array can be
29  * changed, and how string memory is managed with VEX arrays.
30  */
31 
32 #include <UT/UT_DSOVersion.h>
33 #include <UT/UT_Array.h>
34 #include <UT/UT_Vector3.h>
35 #include <UT/UT_Vector4.h>
36 #include <UT/UT_Matrix3.h>
37 #include <UT/UT_Matrix4.h>
38 #include <UT/UT_StringHolder.h>
39 #include <UT/UT_Assert.h>
40 #include <VEX/VEX_VexOp.h>
41 
42 using namespace UT::Literal;
43 
44 namespace HDK_Sample {
45 
46 template <typename T>
47 static bool
48 compareValues(const T &a, const T &b)
49 {
50  return &a > &b;
51 }
52 
53 template <>
54 bool
55 compareValues<const char *>(const char * const &a, const char * const &b)
56 {
57  return a > b;
58 }
59 
60 template <>
61 bool
63 {
64  return a.length2() > b.length2();
65 }
66 
67 template <>
68 bool
70 {
71  return a.length2() > b.length2();
72 }
73 
74 template <>
75 bool
77 {
78  return a.determinant() > b.determinant();
79 }
80 
81 template <>
82 bool
84 {
85  return a.determinant() > b.determinant();
86 }
87 
88 template <typename T>
89 static void
90 sort(int argc, void *argv[], void *)
91 {
92  UT_Array<T> *arr = (UT_Array<T> *)argv[0];
93 
94  // This will work with all types (including strings). Since no strings
95  // are created or destroyed by this method, it's not necessary to call
96  // VEX_VexOp::stringAlloc() or VEX_VexOp::stringFree() to free them -
97  // the pointers are just reordered in the array.
98  arr->sort(compareValues<T>);
99 }
100 
101 static void
102 decimate(int argc, void *argv[], void *)
103 {
106 
107  UT_ASSERT(dst.entries() == 0);
108  for (int i = 0; i < src.entries()/2; i++)
109  {
110  // Acquire a new reference to the string for storage in the
111  // destination array.
112  const char *str = VEX_VexOp::stringAlloc(src(i*2));
113 
114  dst.append(str);
115  }
116 }
117 
118 }
119 
120 //
121 // Installation function
122 //
123 using namespace HDK_Sample;
124 void
125 newVEXOp(void *)
126 {
127  // Sort the array by value for scalars, length for vectors, or
128  // determinant for matrices.
129  new VEX_VexOp("hdksort@*[I"_sh, // Signature
130  sort<VEXint<VEX_32>>); // Evaluator
131  new VEX_VexOp("hdksort@*[F"_sh, // Signature
132  sort<VEXfloat<VEX_32>>); // Evaluator
133  new VEX_VexOp("hdksort@*[V"_sh, // Signature
134  sort<VEXvec3<VEX_32>>); // Evaluator
135  new VEX_VexOp("hdksort@*[P"_sh, // Signature
136  sort<VEXvec4<VEX_32>>); // Evaluator
137  new VEX_VexOp("hdksort@*[3"_sh, // Signature
138  sort<VEXmat3<VEX_32>>); // Evaluator
139  new VEX_VexOp("hdksort@*[4"_sh, // Signature
140  sort<VEXmat4<VEX_32>>); // Evaluator
141  new VEX_VexOp("hdksort@*[S"_sh, // Signature
142  sort<const char *>); // Evaluator
143 
144  // Remove every second string in the source array and store the result
145  // in the destination.
146  new VEX_VexOp("hdkdecimate@&[S[S"_sh, // Signature
147  decimate); // Evaluator
148 }
149 
bool compareValues< const char * >(const char *const &a, const char *const &b)
Definition: VEX_Sort.C:55
Use this class to extend VEX by adding custom VEX functions.
Definition: VEX_VexOp.h:150
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
typename VEX_PrecisionResolver< P >::vec3_type VEXvec3
Definition: VEX_PodTypes.h:70
void sort(ComparatorBool is_less={})
Sort using std::sort with bool comparator. Defaults to operator<().
Definition: UT_Array.h:456
typename VEX_PrecisionResolver< P >::float_type VEXfloat
Definition: VEX_PodTypes.h:67
typename VEX_PrecisionResolver< P >::mat4_type VEXmat4
Definition: VEX_PodTypes.h:74
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
exint append()
Definition: UT_Array.h:142
bool compareValues< UT_Matrix3 >(const UT_Matrix3 &a, const UT_Matrix3 &b)
Definition: VEX_Sort.C:76
exint entries() const
Alias of size(). size() is preferred.
Definition: UT_Array.h:648
GLenum GLenum dst
Definition: glcorearb.h:1793
bool compareValues< UT_Vector4 >(const UT_Vector4 &a, const UT_Vector4 &b)
Definition: VEX_Sort.C:69
bool compareValues< UT_Vector3 >(const UT_Vector3 &a, const UT_Vector3 &b)
Definition: VEX_Sort.C:62
bool compareValues< UT_Matrix4 >(const UT_Matrix4 &a, const UT_Matrix4 &b)
Definition: VEX_Sort.C:83
typename VEX_PrecisionResolver< P >::vec4_type VEXvec4
Definition: VEX_PodTypes.h:71
typename VEX_PrecisionResolver< P >::int_type VEXint
Definition: VEX_PodTypes.h:68
static const char * stringAlloc(const char *str)
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
void newVEXOp(void *)
Definition: VEX_Sort.C:125
typename VEX_PrecisionResolver< P >::mat3_type VEXmat3
Definition: VEX_PodTypes.h:73
void sort(I begin, I end, const Pred &pred)
Definition: pugixml.cpp:7334
GLenum src
Definition: glcorearb.h:1793