ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
FloatNodeImpl.cpp
Go to the documentation of this file.
1 /*
2  * Original work Copyright 2009 - 2010 Kevin Ackley (kackley@gwi.net)
3  * Modified work Copyright 2018 - 2020 Andy Maloney <asmaloney@gmail.com>
4  *
5  * Permission is hereby granted, free of charge, to any person or organization
6  * obtaining a copy of the software and accompanying documentation covered by
7  * this license (the "Software") to use, reproduce, display, distribute,
8  * execute, and transmit the Software, and to prepare derivative works of the
9  * Software, and to permit third-parties to whom the Software is furnished to
10  * do so, all subject to the following:
11  *
12  * The copyright notices in the Software and this entire statement, including
13  * the above license grant, this restriction and the following disclaimer,
14  * must be included in all copies of the Software, in whole or in part, and
15  * all derivative works of the Software, unless such copies or derivative
16  * works are solely in the form of machine-executable object code generated by
17  * a source language processor.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
22  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
23  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
24  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25  * DEALINGS IN THE SOFTWARE.
26  */
27 
28 #include "FloatNodeImpl.h"
29 #include "CheckedFile.h"
30 
31 namespace e57
32 {
33  FloatNodeImpl::FloatNodeImpl( ImageFileImplWeakPtr destImageFile, double value, FloatPrecision precision,
34  double minimum, double maximum ) :
35  NodeImpl( destImageFile ), value_( value ), precision_( precision ), minimum_( minimum ), maximum_( maximum )
36  {
37  // don't checkImageFileOpen, NodeImpl() will do it
38 
42  if ( precision_ == E57_SINGLE )
43  {
44  if ( minimum_ < E57_FLOAT_MIN )
45  {
46  minimum_ = E57_FLOAT_MIN;
47  }
48  if ( maximum_ > E57_FLOAT_MAX )
49  {
50  maximum_ = E57_FLOAT_MAX;
51  }
52  }
53 
55  if ( value < minimum || maximum < value )
56  {
58  "this->pathName=" + this->pathName() + " value=" + toString( value ) +
59  " minimum=" + toString( minimum ) + " maximum=" + toString( maximum ) );
60  }
61  }
62 
64  {
65  // don't checkImageFileOpen
66 
68  if ( ni->type() != E57_FLOAT )
69  {
70  return ( false );
71  }
72 
74  std::shared_ptr<FloatNodeImpl> fi( std::static_pointer_cast<FloatNodeImpl>( ni ) );
75 
77  if ( precision_ != fi->precision_ )
78  {
79  return ( false );
80  }
81 
83  if ( minimum_ != fi->minimum_ )
84  {
85  return ( false );
86  }
87 
89  if ( maximum_ != fi->maximum_ )
90  {
91  return ( false );
92  }
93 
95 
97  return ( true );
98  }
99 
100  bool FloatNodeImpl::isDefined( const ustring &pathName )
101  {
102  // don't checkImageFileOpen
103 
105  return pathName.empty();
106  }
107 
108  double FloatNodeImpl::value() const
109  {
110  checkImageFileOpen( __FILE__, __LINE__, static_cast<const char *>( __FUNCTION__ ) );
111  return value_;
112  }
113 
115  {
116  checkImageFileOpen( __FILE__, __LINE__, static_cast<const char *>( __FUNCTION__ ) );
117  return precision_;
118  }
119 
120  double FloatNodeImpl::minimum() const
121  {
122  checkImageFileOpen( __FILE__, __LINE__, static_cast<const char *>( __FUNCTION__ ) );
123  return minimum_;
124  }
125 
126  double FloatNodeImpl::maximum() const
127  {
128  checkImageFileOpen( __FILE__, __LINE__, static_cast<const char *>( __FUNCTION__ ) );
129  return maximum_;
130  }
131 
133  {
134  // don't checkImageFileOpen
135 
138  if ( pathNames.find( relativePathName( origin ) ) == pathNames.end() &&
139  pathNames.find( pathName() ) == pathNames.end() )
140  {
141  throw E57_EXCEPTION2( E57_ERROR_NO_BUFFER_FOR_ELEMENT, "this->pathName=" + this->pathName() );
142  }
143  }
144 
146  const char *forcedFieldName )
147  {
148  // don't checkImageFileOpen
149 
150  ustring fieldName;
151  if ( forcedFieldName != nullptr )
152  {
153  fieldName = forcedFieldName;
154  }
155  else
156  {
157  fieldName = elementName_;
158  }
159 
160  cf << space( indent ) << "<" << fieldName << " type=\"Float\"";
161  if ( precision_ == E57_SINGLE )
162  {
163  cf << " precision=\"single\"";
164 
166  if ( minimum_ > E57_FLOAT_MIN )
167  {
168  cf << " minimum=\"" << static_cast<float>( minimum_ ) << "\"";
169  }
170  if ( maximum_ < E57_FLOAT_MAX )
171  {
172  cf << " maximum=\"" << static_cast<float>( maximum_ ) << "\"";
173  }
174 
176  if ( value_ != 0.0 )
177  {
178  cf << ">" << static_cast<float>( value_ ) << "</" << fieldName << ">\n";
179  }
180  else
181  {
182  cf << "/>\n";
183  }
184  }
185  else
186  {
188 
190  if ( minimum_ > E57_DOUBLE_MIN )
191  {
192  cf << " minimum=\"" << minimum_ << "\"";
193  }
194  if ( maximum_ < E57_DOUBLE_MAX )
195  {
196  cf << " maximum=\"" << maximum_ << "\"";
197  }
198 
200  if ( value_ != 0.0 )
201  {
202  cf << ">" << value_ << "</" << fieldName << ">\n";
203  }
204  else
205  {
206  cf << "/>\n";
207  }
208  }
209  }
210 
211 #ifdef E57_DEBUG
212  void FloatNodeImpl::dump( int indent, std::ostream &os ) const
213  {
214  // don't checkImageFileOpen
215  os << space( indent ) << "type: Float"
216  << " (" << type() << ")" << std::endl;
217  NodeImpl::dump( indent, os );
218  os << space( indent ) << "precision: ";
219  if ( precision() == E57_SINGLE )
220  {
221  os << "single" << std::endl;
222  }
223  else
224  {
225  os << "double" << std::endl;
226  }
227 
229  const std::streamsize oldPrecision = os.precision();
230  const std::ios_base::fmtflags oldFlags = os.flags();
231 
232  os << space( indent ) << std::scientific << std::setprecision( 17 ) << "value: " << value_ << std::endl;
233  os << space( indent ) << "minimum: " << minimum_ << std::endl;
234  os << space( indent ) << "maximum: " << maximum_ << std::endl;
235 
237  os.precision( oldPrecision );
238  os.flags( oldFlags );
239  }
240 #endif
241 }
#define E57_EXCEPTION2(ecode, context)
Definition: Common.h:68
double minimum() const
void checkLeavesInSet(const StringSet &pathNames, NodeImplSharedPtr origin) override
bool isTypeEquivalent(NodeImplSharedPtr ni) override
double value() const
NodeType type() const override
Definition: FloatNodeImpl.h:40
FloatPrecision precision() const
double maximum() const
FloatNodeImpl(ImageFileImplWeakPtr destImageFile, double value=0, FloatPrecision precision=E57_DOUBLE, double minimum=E57_DOUBLE_MIN, double maximum=E57_DOUBLE_MAX)
void dump(int indent=0, std::ostream &os=std::cout) const override
bool isDefined(const ustring &pathName) override
void writeXml(ImageFileImplSharedPtr imf, CheckedFile &cf, int indent, const char *forcedFieldName=nullptr) override
ustring elementName_
Definition: NodeImpl.h:96
ustring relativePathName(const NodeImplSharedPtr &origin, ustring childPathName=ustring()) const
Definition: NodeImpl.cpp:93
void checkImageFileOpen(const char *srcFileName, int srcLineNumber, const char *srcFunctionName) const
Definition: NodeImpl.cpp:41
virtual void dump(int indent=0, std::ostream &os=std::cout) const
Definition: NodeImpl.cpp:372
ustring pathName() const
Definition: NodeImpl.cpp:74
QTextStream & endl(QTextStream &stream)
Definition: QtCompat.h:718
std::shared_ptr< class NodeImpl > NodeImplSharedPtr
Definition: Common.h:190
std::weak_ptr< class ImageFileImpl > ImageFileImplWeakPtr
Definition: Common.h:189
std::shared_ptr< class ImageFileImpl > ImageFileImplSharedPtr
Definition: Common.h:188
FloatPrecision
The IEEE floating point number precisions supported.
Definition: E57Format.h:71
@ E57_SINGLE
32 bit IEEE floating point number format
Definition: E57Format.h:72
@ E57_ERROR_VALUE_OUT_OF_BOUNDS
element value out of min/max bounds
Definition: E57Exception.h:81
@ E57_ERROR_NO_BUFFER_FOR_ELEMENT
Definition: E57Exception.h:69
std::string ustring
UTF-8 encodeded Unicode string.
Definition: E57Format.h:54
std::set< std::string > StringSet
Definition: Common.h:194
@ E57_FLOAT
FloatNode class.
Definition: E57Format.h:64
std::string toString(T x)
Definition: Common.h:80
std::string space(size_t n)
Definition: Common.h:73