File: NMR_ModelSliceStackResource.cpp

package info (click to toggle)
lib3mf 1.8.1%2Bds-5
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 12,636 kB
  • sloc: cpp: 34,988; ansic: 4,255; sh: 109; makefile: 12
file content (238 lines) | stat: -rw-r--r-- 6,449 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
/*++

Copyright (C) 2018 3MF Consortium

All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Abstract:

NMR_ModelSliceStackResource.h: implements the resource object for a slice stack

--*/

#include "Model/Classes/NMR_ModelSliceResource.h"
#include "Model/Classes/NMR_ModelResource.h"
#include "Common/NMR_Exception.h"

namespace NMR {
	CSlice::CSlice() {
		m_topZ = 0.0;
	}

	CSlice::~CSlice() {

	}

	nfUint32 CSlice::beginPolygon() {
		m_Polygons.push_back(std::vector<nfUint32>());
		return (nfUint32)m_Polygons.size() - 1;
	}

	nfUint32 CSlice::addVertex(nfFloat x, nfFloat y) {
		SLICENODE cNode;

		cNode.m_position.m_fields[0] = x;
		cNode.m_position.m_fields[1] = y;

		cNode.m_index = (nfUint32)m_Vertices.size();

		m_Vertices.push_back(cNode);

		return cNode.m_index;
	}

	void CSlice::getVertex(nfUint32 nIndex, nfFloat *x, nfFloat *y) {
		if (nIndex >= m_Vertices.size())
			throw CNMRException(NMR_ERROR_INVALIDINDEX);

		*x = m_Vertices[nIndex].m_position.m_values.x;
		*y = m_Vertices[nIndex].m_position.m_values.y;
	}

	void CSlice::addPolygonIndex(nfUint32 nPolygonIndex, nfUint32 nIndex) {
		if (nPolygonIndex >= m_Polygons.size())
			throw CNMRException(NMR_ERROR_INVALIDINDEX);

		if (nIndex >= m_Vertices.size())
			throw CNMRException(NMR_ERROR_INVALID_SLICESEGMENT_VERTEXINDEX);

		if (m_Polygons[nPolygonIndex].size() > 0) {
			if (m_Polygons[nPolygonIndex][m_Polygons[nPolygonIndex].size()-1] == nIndex)
				throw CNMRException(NMR_ERROR_INVALID_SLICESEGMENT_VERTEXINDEX);
		}
		m_Polygons[nPolygonIndex].push_back(nIndex);
	}

	nfUint32 CSlice::getPolygonCount() {
		return (nfUint32)m_Polygons.size();
	}

	nfFloat CSlice::getTopZ() {
		return m_topZ;
	}

	void CSlice::setTopZ(nfFloat nTopZ) {
		m_topZ = nTopZ;
	}

	_Ret_notnull_ SLICENODE *CSlice::getNode(nfUint32 nIndex) {
		return &m_Vertices[nIndex];
	}

	nfUint32 CSlice::getVertexCount() {
		return (nfUint32)m_Vertices.size();
	}

	bool CSlice::allPolygonsAreClosed() {
		for (auto polygon : m_Polygons) {
			if (polygon.size() > 1) {
				if (*polygon.begin() != *polygon.rbegin()) {
					return false;
				}
			}
		}
		return true;
	}

	bool CSlice::isPolygonValid(nfUint32 nPolygonIndex)
	{
		if (nPolygonIndex >= m_Polygons.size())
			throw CNMRException(NMR_ERROR_INVALIDINDEX);
		if (m_Polygons[nPolygonIndex].size() > 2)
			return true;
		if (m_Polygons[nPolygonIndex].size() <= 1)
			return false;
		// closed polygon must have 3 points or more.
		return m_Polygons[nPolygonIndex][0] != m_Polygons[nPolygonIndex][1];
	}

	nfUint32 CSlice::getNumberOfPolygons() {
		return (nfUint32)m_Polygons.size();
	}

	nfUint32 CSlice::getPolygonIndex(nfUint32 nPolygonIndex, nfUint32 nIndexOfIndex) {
		if (nPolygonIndex >= m_Polygons.size())
			throw CNMRException(NMR_ERROR_INVALIDINDEX);

		if (nIndexOfIndex >= m_Polygons[nPolygonIndex].size())
			throw CNMRException(NMR_ERROR_INVALIDINDEX);

		return m_Polygons[nPolygonIndex][nIndexOfIndex];
	}

	nfUint32 CSlice::getPolygonIndexCount(nfUint32 nPolygonIndex) {
		if (nPolygonIndex >= m_Polygons.size())
			throw CNMRException(NMR_ERROR_INVALIDINDEX);

		return (nfUint32)m_Polygons[nPolygonIndex].size();
	}


	CModelSliceStackResource::CModelSliceStackResource(_In_ const ModelResourceID sID, _In_ CModel * pModel, PSliceStack pSliceStack) : CModelResource(sID, pModel) 
	{
		m_pSliceStack = pSliceStack;
		m_nNumSliceRefsToMe = 0;
	}

	CModelSliceStackResource::~CModelSliceStackResource() {

	}

	void CModelSliceStackResource::setSliceStack(PSliceStack pSliceStack) {
		m_pSliceStack = pSliceStack;
	}

	_Ret_notnull_ PSliceStack CModelSliceStackResource::getSliceStack() {
		return m_pSliceStack;
	}

	nfUint32& CModelSliceStackResource::NumSliceRefsToMe()
	{
		return m_nNumSliceRefsToMe;
	}

	std::string CModelSliceStackResource::sliceRefPath() {
		return getSliceStack()->usesSliceRef() ? "/2D/2dmodel_" + std::to_string(getResourceID()->getUniqueID()) + ".model" : "";
	}

	CSliceStack::CSliceStack() {
		m_BottomZ = 0.0;
		m_bUsesSliceRef = false;
	}

	CSliceStack::~CSliceStack() {
	}

	_Ret_notnull_ PSlice CSliceStack::getSlice(nfUint32 nIndex) {
		return m_Slices[nIndex];
	}

	nfUint32 CSliceStack::addSlice(PSlice pSlice) {
		if (pSlice->getTopZ() < m_BottomZ)
			throw CNMRException(NMR_ERROR_SLICES_Z_NOTINCREASING);
		if (!m_Slices.empty()) {
			if (pSlice->getTopZ() < m_Slices.rbegin()->get()->getTopZ() ) {
				throw CNMRException(NMR_ERROR_SLICES_Z_NOTINCREASING);
			}
		}
		m_Slices.push_back(pSlice);
		return (nfUint32)m_Slices.size() - 1;
	}

	void CSliceStack::mergeSliceStack(PSliceStack pSliceStack)
	{
		for (int i = 0; i < (int)pSliceStack->getSliceCount(); i++)
		{
			this->addSlice(pSliceStack->getSlice(i));
		}
	}

	nfUint32 CSliceStack::getSliceCount() {
		return (nfUint32)m_Slices.size();
	}

	nfFloat CSliceStack::getBottomZ() {
		return m_BottomZ;
	}

	void CSliceStack::setBottomZ(nfFloat nBottomZ) {
		m_BottomZ = nBottomZ;
	}

	void CSliceStack::setUsesSliceRef(nfBool bUsesSliceRef) {
		m_bUsesSliceRef = bUsesSliceRef;
	}

	nfBool CSliceStack::usesSliceRef() {
		return m_bUsesSliceRef;
	}

	bool CSliceStack::areAllPolygonsClosed() {
		for (auto pSlice : m_Slices) {
			if (!pSlice->allPolygonsAreClosed())
				return false;
		}
		return true;
	}
}