File: NMR_VectorTree.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 (191 lines) | stat: -rw-r--r-- 5,355 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
/*++

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_VectorTree.cpp implements a n*log(n) lookup tree class to identify vectors
by their position.

--*/

#include "Common/Math/NMR_VectorTree.h" 
#include "Common/Math/NMR_Vector.h" 
#include "Common/NMR_Exception.h" 
#include <cmath>
#include <map>

namespace NMR {

	bool operator< (_In_ const VECTORTREEENTRY & entry1, _In_ const VECTORTREEENTRY & entry2)
	{
		for (int i = 0; i < 3; i++) {
			if (entry1.m_position.m_fields[i] < entry2.m_position.m_fields[i])
				return true;
			if (entry1.m_position.m_fields[i] > entry2.m_position.m_fields[i])
				return false;
		}

		return false;
	}

	CVectorTree::CVectorTree()
	{
		setUnits(NMR_VECTOR_DEFAULTUNITS);
	}

	CVectorTree::CVectorTree(_In_ nfFloat fUnits)
	{
		setUnits(fUnits);
	}

	nfFloat CVectorTree::getUnits()
	{
		return m_fUnits;
	}

	void CVectorTree::setUnits(_In_ nfFloat fUnits)
	{
		if ((fUnits < NMR_VECTOR_MINUNITS) || (fUnits > NMR_VECTOR_MAXUNITS))
			throw CNMRException(NMR_ERROR_INVALIDUNITS);
		if (m_entries.size() > 0)
			throw CNMRException(NMR_ERROR_COULDNOTSETUNITS);

		m_fUnits = fUnits;
	}

	_Success_(return) nfBool CVectorTree::findVector2(_In_ NVEC2 vVector, _Out_opt_ nfUint32 & value)
	{
		NVEC3 vVector3;
		vVector3.m_fields[0] = vVector.m_fields[0];
		vVector3.m_fields[1] = vVector.m_fields[1];
		vVector3.m_fields[2] = 0.0f;
		return findVector3(vVector3, value);
	}

	_Success_(return) nfBool CVectorTree::findVector3(_In_ NVEC3 vVector, _Out_opt_ nfUint32 & value)
	{
		VECTORTREEENTRY entry;
		entry.m_position = fnVEC3I_floor(vVector, m_fUnits);

		std::map <VECTORTREEENTRY, nfUint32>::iterator iter = m_entries.find(entry);

		if (iter != m_entries.end()) {
			value = iter->second;
			return true;
		}
		else
			return false;
	}

	_Success_(return) nfBool CVectorTree::findIntVector2(_In_ NVEC2I vVector, _Out_opt_ nfUint32 & value)
	{
		NVEC3I vVector3;
		vVector3.m_fields[0] = vVector.m_fields[0];
		vVector3.m_fields[1] = vVector.m_fields[1];
		vVector3.m_fields[2] = 0;
		return findIntVector3(vVector3, value);
	}

	_Success_(return) nfBool CVectorTree::findIntVector3(_In_ NVEC3I vVector, _Out_opt_ nfUint32 & value)
	{
		VECTORTREEENTRY entry;
		entry.m_position = vVector;

		std::map <VECTORTREEENTRY, nfUint32>::iterator iter = m_entries.find(entry);

		if (iter != m_entries.end()) {
			value = iter->second;
			return true;
		}
		else
			return false;
	}

	void CVectorTree::addVector2(_In_ NVEC2 vVector, _In_ nfUint32 value)
	{
		NVEC3 vVector3;
		vVector3.m_fields[0] = vVector.m_fields[0];
		vVector3.m_fields[1] = vVector.m_fields[1];
		vVector3.m_fields[2] = 0.0f;
		addVector3(vVector3, value);
	}

	void CVectorTree::addVector3(_In_ NVEC3 vVector, _In_ nfUint32 value)
	{
		VECTORTREEENTRY entry;
		entry.m_position = fnVEC3I_floor(vVector, m_fUnits);
		m_entries.insert(std::make_pair(entry, value));
	}

	void CVectorTree::addIntVector2(_In_ NVEC2I vVector, _In_ nfUint32 value)
	{
		NVEC3I vVector3;
		vVector3.m_fields[0] = vVector.m_fields[0];
		vVector3.m_fields[1] = vVector.m_fields[1];
		vVector3.m_fields[2] = 0;
		addIntVector3(vVector3, value);
	}

	void CVectorTree::addIntVector3(_In_ NVEC3I vVector, _In_ nfUint32 value)
	{
		VECTORTREEENTRY entry;
		entry.m_position = vVector;
		m_entries.insert(std::make_pair(entry, value));
	}

	void CVectorTree::removeVector2(_In_ NVEC2 vVector)
	{
		NVEC3 vVector3;
		vVector3.m_fields[0] = vVector.m_fields[0];
		vVector3.m_fields[1] = vVector.m_fields[1];
		vVector3.m_fields[2] = 0.0f;
		removeVector3(vVector3);
	}

	void CVectorTree::removeVector3(_In_ NVEC3 vVector)
	{
		VECTORTREEENTRY entry;
		entry.m_position = fnVEC3I_floor(vVector, m_fUnits);
		m_entries.erase(entry);
	}

	void CVectorTree::removeIntVector2(_In_ NVEC2I vVector)
	{
		NVEC3I vVector3;
		vVector3.m_fields[0] = vVector.m_fields[0];
		vVector3.m_fields[1] = vVector.m_fields[1];
		vVector3.m_fields[2] = 0;
		removeIntVector3(vVector3);
	}

	void CVectorTree::removeIntVector3(_In_ NVEC3I vVector)
	{
		VECTORTREEENTRY entry;
		entry.m_position = vVector;
		m_entries.erase(entry);
	}

}