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
|
/*++
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_ModelReaderNode093_Triangle.cpp implements the Model Reader Triangle Node
Class. A triangle reader model node is a parser for the triangle node of an
XML Model Stream.
--*/
#include "Model/Reader/v093/NMR_ModelReaderNode093_Triangle.h"
#include "Model/Classes/NMR_ModelConstants.h"
#include "Common/NMR_Exception.h"
#include "Common/NMR_Exception_Windows.h"
#include "Common/NMR_StringUtils.h"
#include <climits>
#include <cmath>
#include <wchar.h>
namespace NMR {
CModelReaderNode093_Triangle::CModelReaderNode093_Triangle(_In_ PModelReaderWarnings pWarnings)
: CModelReaderNode(pWarnings)
{
// Initialise default values
m_nIndex1 = -1;
m_nIndex2 = -1;
m_nIndex3 = -1;
m_nTextureIndex1 = -1;
m_nTextureIndex2 = -1;
m_nTextureIndex3 = -1;
m_nColorID1 = 0;
m_nColorID2 = 0;
m_nColorID3 = 0;
m_nMaterialID = 0;
m_nTextureID = 0;
}
void CModelReaderNode093_Triangle::parseXML(_In_ CXmlReader * pXMLReader)
{
// Parse name
parseName(pXMLReader);
// Parse attribute
parseAttributes(pXMLReader);
// Parse Content
parseContent(pXMLReader);
}
void CModelReaderNode093_Triangle::retrieveIndices(_Out_ nfInt32 & nIndex1, _Out_ nfInt32 & nIndex2, _Out_ nfInt32 & nIndex3, nfInt32 nNodeCount)
{
if ((m_nIndex1 < 0) || (m_nIndex2 < 0) || (m_nIndex3 < 0))
throw CNMRException(NMR_ERROR_INVALIDMODELNODEINDEX);
if ((m_nIndex1 >= nNodeCount) || (m_nIndex2 >= nNodeCount) || (m_nIndex3 >= nNodeCount))
throw CNMRException(NMR_ERROR_INVALIDMODELNODEINDEX);
nIndex1 = m_nIndex1;
nIndex2 = m_nIndex2;
nIndex3 = m_nIndex3;
}
nfBool CModelReaderNode093_Triangle::retrieveTextureIndices(_Out_ nfInt32 & nIndex1, _Out_ nfInt32 & nIndex2, _Out_ nfInt32 & nIndex3)
{
nIndex1 = m_nTextureIndex1;
nIndex2 = m_nTextureIndex2;
nIndex3 = m_nTextureIndex3;
return ((nIndex1 >= 0) && (nIndex2 >= 0) && (nIndex3 >= 0));
}
void CModelReaderNode093_Triangle::OnAttribute(_In_z_ const nfChar * pAttributeName, _In_z_ const nfChar * pAttributeValue)
{
__NMRASSERT(pAttributeName);
__NMRASSERT(pAttributeValue);
nfInt32 nValue;
if (strcmp(pAttributeName, XML_3MF_ATTRIBUTE_TRIANGLE_V1) == 0) {
nValue = fnStringToInt32(pAttributeValue);
if ((nValue >= 0) && (nValue < XML_3MF_MAXRESOURCEINDEX))
m_nIndex1 = nValue;
}
if (strcmp(pAttributeName, XML_3MF_ATTRIBUTE_TRIANGLE_V2) == 0) {
nValue = fnStringToInt32(pAttributeValue);
if ((nValue >= 0) && (nValue < XML_3MF_MAXRESOURCEINDEX))
m_nIndex2 = nValue;
}
if (strcmp(pAttributeName, XML_3MF_ATTRIBUTE_TRIANGLE_V3) == 0) {
nValue = fnStringToInt32(pAttributeValue);
if ((nValue >= 0) && (nValue < XML_3MF_MAXRESOURCEINDEX))
m_nIndex3 = nValue;
}
if (strcmp(pAttributeName, XML_3MF_ATTRIBUTE_TRIANGLE_CV1) == 0) {
nValue = fnStringToInt32(pAttributeValue);
if ((nValue >= 0) && (nValue < XML_3MF_MAXRESOURCEINDEX))
m_nTextureIndex1 = nValue;
}
if (strcmp(pAttributeName, XML_3MF_ATTRIBUTE_TRIANGLE_CV2) == 0) {
nValue = fnStringToInt32(pAttributeValue);
if ((nValue >= 0) && (nValue < XML_3MF_MAXRESOURCEINDEX))
m_nTextureIndex2 = nValue;
}
if (strcmp(pAttributeName, XML_3MF_ATTRIBUTE_TRIANGLE_CV3) == 0) {
nValue = fnStringToInt32(pAttributeValue);
if ((nValue >= 0) && (nValue < XML_3MF_MAXRESOURCEINDEX))
m_nTextureIndex3 = nValue;
}
if (strcmp(pAttributeName, XML_3MF_ATTRIBUTE_TRIANGLE_COLORID) == 0) {
std::string sValue(pAttributeValue);
std::string sSubStr = sValue.substr(0, 4);
if (sSubStr == "tex(") {
if (sValue.length() > 4) {
std::string sTexID = sValue.substr(4, sValue.length() - sSubStr.length() - 1);
nfInt32 nValue = fnStringToInt32(sTexID.c_str());
if ((nValue < 0) || (nValue >= XML_3MF_MAXRESOURCEINDEX))
throw CNMRException(NMR_ERROR_INVALIDTEXTUREREFERENCE);
m_nTextureID = nValue + 1;
}
else {
throw CNMRException(NMR_ERROR_INVALIDTEXTUREREFERENCE);
}
}
else {
const nfChar * pCommaValue = strchr(pAttributeValue, ',');
// Check, if we have a single value
if (pCommaValue == nullptr) {
nValue = fnStringToInt32(pAttributeValue);
if ((nValue >= 0) && (nValue < XML_3MF_MAXRESOURCEINDEX)) {
m_nColorID1 = nValue + 1;
m_nColorID2 = nValue + 1;
m_nColorID3 = nValue + 1;
}
}
else {
// Check, if we have a color triplet
nfInt32 nColorID1 = -1;
nfInt32 nColorID2 = -1;
nfInt32 nColorID3 = -1;
fnStringToCommaSeparatedIntegerTriplet(pAttributeValue, nColorID1, nColorID2, nColorID3);
if ((nColorID1 >= 0) && (nColorID1 < XML_3MF_MAXRESOURCEINDEX))
m_nColorID1 = nColorID1 + 1;
if ((nColorID2 >= 0) && (nColorID2 < XML_3MF_MAXRESOURCEINDEX))
m_nColorID2 = nColorID2 + 1;
if ((nColorID3 >= 0) && (nColorID3 < XML_3MF_MAXRESOURCEINDEX))
m_nColorID3 = nColorID3 + 1;
}
}
}
if (strcmp(pAttributeName, XML_3MF_ATTRIBUTE_TRIANGLE_MATERIALID) == 0) {
nValue = fnStringToInt32(pAttributeValue);
if ((nValue >= 0) && (nValue < XML_3MF_MAXRESOURCEINDEX))
m_nMaterialID = nValue + 1;
}
}
nfBool CModelReaderNode093_Triangle::retrieveColorIDs(_Out_ nfInt32 & nColorID1, _Out_ nfInt32 & nColorID2, _Out_ nfInt32 & nColorID3)
{
nColorID1 = m_nColorID1;
nColorID2 = m_nColorID2;
nColorID3 = m_nColorID3;
return ((nColorID1 != 0) && (nColorID2 != 0) && (nColorID3 != 0));
}
nfInt32 CModelReaderNode093_Triangle::retrieveTextureID()
{
return m_nTextureID;
}
nfInt32 CModelReaderNode093_Triangle::retrieveMaterialID()
{
return m_nMaterialID;
}
}
|