Debian Codegraph @

plugins/script/interfaces/PatchInterface.cpp

raw
1 #include "PatchInterface.h"
2
3 #include <pybind11/pybind11.h>
4 #include <pybind11/stl_bind.h>
5
6 #include "ipatch.h"
7 #include "itextstream.h"
8
9 #include "../SceneNodeBuffer.h"
10
11 namespace script
12 {
13
14 ScriptPatchNode::ScriptPatchNode(const scene::INodePtr& node) :
15 ScriptSceneNode((node != NULL && Node_isPatch(node)) ? node : scene::INodePtr())
16 {}
17
18 bool ScriptPatchNode::isPatch(const ScriptSceneNode& node)
19 {
20 return Node_isPatch(node);
21 }
22
23 ScriptPatchNode ScriptPatchNode::getPatch(const ScriptSceneNode& node)
24 {
25 // Try to cast the node onto a patch
26 IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(
27 static_cast<scene::INodePtr>(node)
28 );
29
30 // Construct a patchNode (contained node may be NULL)
31 return (patchNode != NULL) ? ScriptPatchNode(node) : ScriptPatchNode(scene::INodePtr());
32 }
33
34 void ScriptPatchNode::setDims(std::size_t width, std::size_t height)
35 {
36 IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
37 if (patchNode == NULL) return;
38
39 patchNode->getPatch().setDims(width, height);
40 }
41
42 std::size_t ScriptPatchNode::getWidth() const
43 {
44 IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
45 if (patchNode == NULL) return 0;
46
47 return patchNode->getPatch().getWidth();
48 }
49
50 std::size_t ScriptPatchNode::getHeight() const
51 {
52 IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
53 if (patchNode == NULL) return 0;
54
55 return patchNode->getPatch().getHeight();
56 }
57
58 PatchMesh ScriptPatchNode::getTesselatedPatchMesh() const
59 {
60 IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
61 if (patchNode == NULL) return PatchMesh();
62
63 return patchNode->getPatch().getTesselatedPatchMesh();
64 }
65
66 PatchControl& ScriptPatchNode::ctrlAt(std::size_t row, std::size_t col)
67 {
68 IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
69 if (patchNode == NULL) return _emptyPatchControl;
70
71 IPatch& patch = patchNode->getPatch();
72
73 if (row > patch.getHeight() || col > patch.getWidth())
74 {
75 rError() << "One or more patch control indices out of bounds: " << row << "," << col << std::endl;
76 return _emptyPatchControl;
77 }
78
79 return patchNode->getPatch().ctrlAt(row, col);
80 }
81
82 void ScriptPatchNode::insertColumns(std::size_t colIndex)
83 {
84 IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
85 if (patchNode == NULL) return;
86
87 return patchNode->getPatch().insertColumns(colIndex);
88 }
89
90 void ScriptPatchNode::insertRows(std::size_t rowIndex)
91 {
92 IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
93 if (patchNode == NULL) return;
94
95 return patchNode->getPatch().insertRows(rowIndex);
96 }
97
98 void ScriptPatchNode::removePoints(int columns, std::size_t index)
99 {
100 IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
101 if (patchNode == NULL) return;
102
103 return patchNode->getPatch().removePoints(static_cast<bool>(columns), index);
104 }
105
106 void ScriptPatchNode::appendPoints(int columns, int beginning)
107 {
108 IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
109 if (patchNode == NULL) return;
110
111 return patchNode->getPatch().appendPoints(static_cast<bool>(columns), static_cast<bool>(beginning));
112 }
113
114 bool ScriptPatchNode::isValid() const
115 {
116 IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
117 if (patchNode == NULL) return false;
118
119 return patchNode->getPatch().isValid();
120 }
121
122 bool ScriptPatchNode::isDegenerate() const
123 {
124 IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
125 if (patchNode == NULL) return true;
126
127 return patchNode->getPatch().isDegenerate();
128 }
129
130 void ScriptPatchNode::controlPointsChanged()
131 {
132 IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
133 if (patchNode == NULL) return;
134
135 patchNode->getPatch().controlPointsChanged();
136 }
137
138 const std::string& ScriptPatchNode::getShader() const
139 {
140 IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
141 if (patchNode == NULL) return _emptyShader;
142
143 return patchNode->getPatch().getShader();
144 }
145
146 void ScriptPatchNode::setShader(const std::string& name)
147 {
148 IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
149 if (patchNode == NULL) return;
150
151 patchNode->getPatch().setShader(name);
152 }
153
154 bool ScriptPatchNode::hasVisibleMaterial()
155 {
156 IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
157 if (patchNode == NULL) return false;
158
159 return patchNode->getPatch().hasVisibleMaterial();
160 }
161
162 bool ScriptPatchNode::subdivisionsFixed() const
163 {
164 IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
165 if (patchNode == NULL) return false;
166
167 return patchNode->getPatch().subdivisionsFixed();
168 }
169
170 Subdivisions ScriptPatchNode::getSubdivisions() const
171 {
172 IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
173 if (patchNode == NULL) return Subdivisions();
174
175 return patchNode->getPatch().getSubdivisions();
176 }
177
178 void ScriptPatchNode::setFixedSubdivisions(int isFixed, const Subdivisions& divisions)
179 {
180 IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
181 if (patchNode == NULL) return;
182
183 patchNode->getPatch().setFixedSubdivisions(static_cast<bool>(isFixed), divisions);
184 }
185
186 // Initialise static members
187 const std::string ScriptPatchNode::_emptyShader;
188 PatchControl ScriptPatchNode::_emptyPatchControl;
189
190 ScriptSceneNode PatchInterface::createPatchDef2()
191 {
192 // Create a new patch and return the script scene node
193 scene::INodePtr node = GlobalPatchModule().createPatch(patch::PatchDefType::Def2);
194
195 // Add the node to the buffer otherwise it will be deleted immediately,
196 // as ScriptSceneNodes are using weak_ptrs.
197 SceneNodeBuffer::Instance().push_back(node);
198
199 return ScriptSceneNode(node);
200 }
201
202 ScriptSceneNode PatchInterface::createPatchDef3()
203 {
204 // Create a new patch and return the script scene node
205 scene::INodePtr node = GlobalPatchModule().createPatch(patch::PatchDefType::Def3);
206
207 // Add the node to the buffer otherwise it will be deleted immediately,
208 // as ScriptSceneNodes are using weak_ptrs.
209 SceneNodeBuffer::Instance().push_back(node);
210
211 return ScriptSceneNode(node);
212 }
213
214 void PatchInterface::registerInterface(py::module& scope, py::dict& globals)
215 {
216 py::class_<PatchControl> patchControl(scope, "PatchMeshControl");
217 patchControl.def_readwrite("vertex", &PatchControl::vertex);
218 patchControl.def_readwrite("texcoord", &PatchControl::texcoord);
219
220 py::class_<Subdivisions> subdivisions(scope, "Subdivisions");
221 subdivisions.def(py::init<unsigned int, unsigned int>());
222 subdivisions.def(py::init<const Subdivisions&>());
223
224 // greebo: Pick the correct overload - this is hard to read, but it is necessary
225 subdivisions.def("x", static_cast<unsigned int& (Subdivisions::*)()>(&Subdivisions::x),
226 py::return_value_policy::reference);
227 subdivisions.def("y", static_cast<unsigned int& (Subdivisions::*)()>(&Subdivisions::y),
228 py::return_value_policy::reference);
229
230 py::class_<VertexNT> patchVertex(scope, "PatchMeshVertex");
231
232 patchVertex.def(py::init<>());
233 patchVertex.def_readwrite("vertex", &VertexNT::vertex);
234 patchVertex.def_readwrite("texcoord", &VertexNT::texcoord);
235 patchVertex.def_readwrite("normal", &VertexNT::normal);
236
237 // Declare the VertexNT vector
238 py::bind_vector< std::vector<VertexNT> >(scope, "PatchMeshVertices");
239
240 py::class_<PatchMesh> patchMesh(scope, "PatchMesh");
241 patchMesh.def(py::init<>());
242 patchMesh.def_readonly("width", &PatchMesh::width);
243 patchMesh.def_readonly("height", &PatchMesh::height);
244 patchMesh.def_readonly("vertices", &PatchMesh::vertices);
245
246 // Define a PatchNode interface
247 py::class_<ScriptPatchNode, ScriptSceneNode> patchNode(scope, "PatchNode");
248
249 patchNode.def(py::init<const scene::INodePtr&>());
250 patchNode.def("setDims", &ScriptPatchNode::setDims);
251 patchNode.def("getWidth", &ScriptPatchNode::getWidth);
252 patchNode.def("getHeight", &ScriptPatchNode::getHeight);
253 patchNode.def("ctrlAt", &ScriptPatchNode::ctrlAt, py::return_value_policy::reference_internal);
254 patchNode.def("insertColumns", &ScriptPatchNode::insertColumns);
255 patchNode.def("insertRows", &ScriptPatchNode::insertRows);
256 patchNode.def("removePoints", &ScriptPatchNode::removePoints);
257 patchNode.def("appendPoints", &ScriptPatchNode::appendPoints);
258 patchNode.def("isValid", &ScriptPatchNode::isValid);
259 patchNode.def("isDegenerate", &ScriptPatchNode::isDegenerate);
260 patchNode.def("getShader", &ScriptPatchNode::getShader, py::return_value_policy::reference);
261 patchNode.def("setShader", &ScriptPatchNode::setShader);
262 patchNode.def("hasVisibleMaterial", &ScriptPatchNode::hasVisibleMaterial);
263 patchNode.def("subdivionsFixed", &ScriptPatchNode::subdivisionsFixed); // typo used to be there in previous releases, leave it in there for compatibility reasons
264 patchNode.def("subdivisionsFixed", &ScriptPatchNode::subdivisionsFixed);
265 patchNode.def("getSubdivisions", &ScriptPatchNode::getSubdivisions);
266 patchNode.def("setFixedSubdivisions", &ScriptPatchNode::setFixedSubdivisions);
267 patchNode.def("controlPointsChanged", &ScriptPatchNode::controlPointsChanged);
268 patchNode.def("getTesselatedPatchMesh", &ScriptPatchNode::getTesselatedPatchMesh);
269
270 // Define the GlobalPatchCreator interface
271 py::class_<PatchInterface> patchCreator(scope, "PatchCreator");
272
273 patchCreator.def("createPatchDef2", &PatchInterface::createPatchDef2);
274 patchCreator.def("createPatchDef3", &PatchInterface::createPatchDef3);
275
276 // Now point the Python variable "GlobalPatchCreator" to this instance
277 globals["GlobalPatchCreator"] = this;
278 }
279
280 } // namespace script