File: PatchInterface.cpp

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (280 lines) | stat: -rw-r--r-- 9,529 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include "PatchInterface.h"

#include <pybind11/pybind11.h>
#include <pybind11/stl_bind.h>

#include "ipatch.h"
#include "itextstream.h"

#include "../SceneNodeBuffer.h"

namespace script 
{

ScriptPatchNode::ScriptPatchNode(const scene::INodePtr& node) :
	ScriptSceneNode((node != NULL && Node_isPatch(node)) ? node : scene::INodePtr())
{}

bool ScriptPatchNode::isPatch(const ScriptSceneNode& node)
{
	return Node_isPatch(node);
}

ScriptPatchNode ScriptPatchNode::getPatch(const ScriptSceneNode& node) 
{
	// Try to cast the node onto a patch
	IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(
		static_cast<scene::INodePtr>(node)
	);

	// Construct a patchNode (contained node may be NULL)
	return (patchNode != NULL) ? ScriptPatchNode(node) : ScriptPatchNode(scene::INodePtr());
}

void ScriptPatchNode::setDims(std::size_t width, std::size_t height)
{
	IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
	if (patchNode == NULL) return;

	patchNode->getPatch().setDims(width, height);
}

std::size_t ScriptPatchNode::getWidth() const
{
	IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
	if (patchNode == NULL) return 0;

	return patchNode->getPatch().getWidth();
}

std::size_t ScriptPatchNode::getHeight() const
{
	IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
	if (patchNode == NULL) return 0;

	return patchNode->getPatch().getHeight();
}

PatchMesh ScriptPatchNode::getTesselatedPatchMesh() const
{
	IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
	if (patchNode == NULL) return PatchMesh();

	return patchNode->getPatch().getTesselatedPatchMesh();
}

PatchControl& ScriptPatchNode::ctrlAt(std::size_t row, std::size_t col)
{
	IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
	if (patchNode == NULL) return _emptyPatchControl;

	IPatch& patch = patchNode->getPatch();

	if (row > patch.getHeight() || col > patch.getWidth())
	{
		rError() << "One or more patch control indices out of bounds: " << row << "," << col << std::endl;
		return _emptyPatchControl;
	}

	return patchNode->getPatch().ctrlAt(row, col);
}

void ScriptPatchNode::insertColumns(std::size_t colIndex)
{
	IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
	if (patchNode == NULL) return;

	return patchNode->getPatch().insertColumns(colIndex);
}

void ScriptPatchNode::insertRows(std::size_t rowIndex)
{
	IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
	if (patchNode == NULL) return;

	return patchNode->getPatch().insertRows(rowIndex);
}

void ScriptPatchNode::removePoints(int columns, std::size_t index)
{
	IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
	if (patchNode == NULL) return;

	return patchNode->getPatch().removePoints(static_cast<bool>(columns), index);
}

void ScriptPatchNode::appendPoints(int columns, int beginning)
{
	IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
	if (patchNode == NULL) return;

	return patchNode->getPatch().appendPoints(static_cast<bool>(columns), static_cast<bool>(beginning));
}

bool ScriptPatchNode::isValid() const
{
	IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
	if (patchNode == NULL) return false;

	return patchNode->getPatch().isValid();
}

bool ScriptPatchNode::isDegenerate() const
{
	IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
	if (patchNode == NULL) return true;

	return patchNode->getPatch().isDegenerate();
}

void ScriptPatchNode::controlPointsChanged()
{
	IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
	if (patchNode == NULL) return;

	patchNode->getPatch().controlPointsChanged();
}

const std::string& ScriptPatchNode::getShader() const
{
	IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
	if (patchNode == NULL) return _emptyShader;

	return patchNode->getPatch().getShader();
}

void ScriptPatchNode::setShader(const std::string& name)
{
	IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
	if (patchNode == NULL) return;

	patchNode->getPatch().setShader(name);
}

bool ScriptPatchNode::hasVisibleMaterial()
{
	IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
	if (patchNode == NULL) return false;

	return patchNode->getPatch().hasVisibleMaterial();
}

bool ScriptPatchNode::subdivisionsFixed() const
{
	IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
	if (patchNode == NULL) return false;

	return patchNode->getPatch().subdivisionsFixed();
}

Subdivisions ScriptPatchNode::getSubdivisions() const
{
	IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
	if (patchNode == NULL) return Subdivisions();

	return patchNode->getPatch().getSubdivisions();
}

void ScriptPatchNode::setFixedSubdivisions(int isFixed, const Subdivisions& divisions)
{
	IPatchNodePtr patchNode = std::dynamic_pointer_cast<IPatchNode>(_node.lock());
	if (patchNode == NULL) return;

	patchNode->getPatch().setFixedSubdivisions(static_cast<bool>(isFixed), divisions);
}

// Initialise static members
const std::string ScriptPatchNode::_emptyShader;
PatchControl ScriptPatchNode::_emptyPatchControl;

ScriptSceneNode PatchInterface::createPatchDef2()
{
	// Create a new patch and return the script scene node
	scene::INodePtr node = GlobalPatchModule().createPatch(patch::PatchDefType::Def2);

	// Add the node to the buffer otherwise it will be deleted immediately,
	// as ScriptSceneNodes are using weak_ptrs.
	SceneNodeBuffer::Instance().push_back(node);

	return ScriptSceneNode(node);
}

ScriptSceneNode PatchInterface::createPatchDef3()
{
	// Create a new patch and return the script scene node
	scene::INodePtr node = GlobalPatchModule().createPatch(patch::PatchDefType::Def3);

	// Add the node to the buffer otherwise it will be deleted immediately,
	// as ScriptSceneNodes are using weak_ptrs.
	SceneNodeBuffer::Instance().push_back(node);

	return ScriptSceneNode(node);
}

void PatchInterface::registerInterface(py::module& scope, py::dict& globals) 
{
	py::class_<PatchControl> patchControl(scope, "PatchMeshControl");
	patchControl.def_readwrite("vertex", &PatchControl::vertex);
	patchControl.def_readwrite("texcoord", &PatchControl::texcoord);

	py::class_<Subdivisions> subdivisions(scope, "Subdivisions");
	subdivisions.def(py::init<unsigned int, unsigned int>());
	subdivisions.def(py::init<const Subdivisions&>());

	// greebo: Pick the correct overload - this is hard to read, but it is necessary
	subdivisions.def("x", static_cast<unsigned int& (Subdivisions::*)()>(&Subdivisions::x), 
		py::return_value_policy::reference);
	subdivisions.def("y", static_cast<unsigned int& (Subdivisions::*)()>(&Subdivisions::y), 
		py::return_value_policy::reference);

	py::class_<VertexNT> patchVertex(scope, "PatchMeshVertex");

	patchVertex.def(py::init<>());
	patchVertex.def_readwrite("vertex", &VertexNT::vertex);
	patchVertex.def_readwrite("texcoord", &VertexNT::texcoord);
	patchVertex.def_readwrite("normal", &VertexNT::normal);

	// Declare the VertexNT vector
	py::bind_vector< std::vector<VertexNT> >(scope, "PatchMeshVertices");

	py::class_<PatchMesh> patchMesh(scope, "PatchMesh");
	patchMesh.def(py::init<>());
	patchMesh.def_readonly("width", &PatchMesh::width);
	patchMesh.def_readonly("height", &PatchMesh::height);
	patchMesh.def_readonly("vertices", &PatchMesh::vertices);

	// Define a PatchNode interface
	py::class_<ScriptPatchNode, ScriptSceneNode> patchNode(scope, "PatchNode");

	patchNode.def(py::init<const scene::INodePtr&>());
	patchNode.def("setDims", &ScriptPatchNode::setDims);
	patchNode.def("getWidth", &ScriptPatchNode::getWidth);
	patchNode.def("getHeight", &ScriptPatchNode::getHeight);
	patchNode.def("ctrlAt", &ScriptPatchNode::ctrlAt, py::return_value_policy::reference_internal);
	patchNode.def("insertColumns", &ScriptPatchNode::insertColumns);
	patchNode.def("insertRows", &ScriptPatchNode::insertRows);
	patchNode.def("removePoints", &ScriptPatchNode::removePoints);
	patchNode.def("appendPoints", &ScriptPatchNode::appendPoints);
	patchNode.def("isValid", &ScriptPatchNode::isValid);
	patchNode.def("isDegenerate", &ScriptPatchNode::isDegenerate);
	patchNode.def("getShader", &ScriptPatchNode::getShader, py::return_value_policy::reference);
	patchNode.def("setShader", &ScriptPatchNode::setShader);
	patchNode.def("hasVisibleMaterial", &ScriptPatchNode::hasVisibleMaterial);
	patchNode.def("subdivionsFixed", &ScriptPatchNode::subdivisionsFixed); // typo used to be there in previous releases, leave it in there for compatibility reasons
	patchNode.def("subdivisionsFixed", &ScriptPatchNode::subdivisionsFixed);
	patchNode.def("getSubdivisions", &ScriptPatchNode::getSubdivisions);
	patchNode.def("setFixedSubdivisions", &ScriptPatchNode::setFixedSubdivisions);
	patchNode.def("controlPointsChanged", &ScriptPatchNode::controlPointsChanged);
	patchNode.def("getTesselatedPatchMesh", &ScriptPatchNode::getTesselatedPatchMesh);

	// Define the GlobalPatchCreator interface
	py::class_<PatchInterface> patchCreator(scope, "PatchCreator");

	patchCreator.def("createPatchDef2", &PatchInterface::createPatchDef2);
	patchCreator.def("createPatchDef3", &PatchInterface::createPatchDef3);

	// Now point the Python variable "GlobalPatchCreator" to this instance
	globals["GlobalPatchCreator"] = this;
}

} // namespace script