CFx SDK Documentation  2023 SP0
LODAlgorithm.h
Go to the documentation of this file.
1 // Copyright (C) 2002-2017, Open Design Alliance (the "Alliance").
3 // All rights reserved.
4 //
5 // This software and its documentation and related materials are owned by
6 // the Alliance. The software may only be incorporated into application
7 // programs owned by members of the Alliance, subject to a signed
8 // Membership Agreement and Supplemental Software License Agreement with the
9 // Alliance. The structure and organization of this software are the valuable
10 // trade secrets of the Alliance and its suppliers. The software is also
11 // protected by copyright law and international treaty provisions. Application
12 // programs incorporating this software must include the following statement
13 // with their copyright notices:
14 //
15 // This application incorporates Teigha(R) software pursuant to a license
16 // agreement with Open Design Alliance.
17 // Teigha(R) Copyright (C) 2002-2017 by Open Design Alliance.
18 // All rights reserved.
19 //
20 // By use of this software, its documentation or related materials, you
21 // acknowledge and accept the above terms.
23 
24 #include <iostream>
25 #include "OdPlatformStreamer.h"
26 
27 namespace LODAlgorithm {
28 
29 namespace Clustering {
30 
31 void compress(const OdUInt32& nPoints,
32  const OdUInt8& level,
33  const OdGeExtents3d& ext,
34  OdStreamBuf& origBuf,
35  OdStreamBuf& targetBuf,
36  const bool isLog = false)
37 {
38  OdUInt8 mask = 0x80;
39  unsigned int byte = 0;
40  OdUInt32 bytesCnt = 0;
41  int comp = 0;
42 
43  double min[3] = {ext.minPoint().x, ext.minPoint().y, ext.minPoint().z};
44  double max[3] = {ext.maxPoint().x, ext.maxPoint().y, ext.maxPoint().z};
45 
46  if (isLog) {
47  printf("LOD Start compression\n");
48  printf("EXT MIN: %f %f %f\nEXT MAX: %f %f %f\n", min[0], min[1], min[2], max[0], max[1], max[2]);
49  printf("N points: %d Level: %d\n", nPoints, level);
50  }
51 
52  OdUInt32 valCnt = nPoints * 3;
53  while(valCnt--) {
54  double value = OdPlatformStreamer::rdDouble(origBuf);
55 
56  OdUInt32 clstrValue = floor(((pow(2.0, level) - 1) / (max[comp] - min[comp])) * (value - min[comp]) + 0.5);
57 
58  //calc component axis
59  comp = (comp >= 2) ? 0 : comp + 1;
60 
61  if (isLog)
62  printf("v:%f n:%06d ", value, clstrValue);
63 
64  for (int i = level - 1; i >= 0; --i) {
65  unsigned int bit = clstrValue >> i & 1;
66  byte = OdUInt8(bit != 0 ? (byte|mask) : (byte & (~mask)));
67 
68  //seek
69  mask >>= 1;
70  if(!mask)
71  {
72  mask = 0x80;
73  targetBuf.putByte(byte);
74  bytesCnt++;
75  byte = 0;
76  }
77  }
78  }
79 
80  if(byte != 0)
81  targetBuf.putByte(byte);
82 
83  if (isLog)
84  printf("End compression\nBytes written: %d\n", ++bytesCnt);
85 }
86 
87 void decompress(const OdUInt32& nPoints,
88  const OdUInt8& level,
89  const OdGeExtents3d& ext,
90  OdStreamBuf& origBuf,
91  OdStreamBuf& targetBuf,
92  const bool isLog = false)
93 {
94  double min[3] = {ext.minPoint().x, ext.minPoint().y, ext.minPoint().z};
95  double max[3] = {ext.maxPoint().x, ext.maxPoint().y, ext.maxPoint().z};
96 
97  if (isLog) {
98  printf("LOD Start decompression\n");
99  printf("EXT MIN: %f %f %f\nEXT MAX: %f %f %f\n", min[0], min[1], min[2], max[0], max[1], max[2]);
100  printf("N points: %d Level: %d\n", nPoints, level);
101  }
102 
103  unsigned int byte = origBuf.getByte();
104  OdUInt8 mask = 0x80;
105  OdUInt32 value = 0;
106  OdUInt8 comp = 0;
107  OdUInt8 bitShift = 7;
108  OdUInt32 bytesCnt = 1;
109 
110  for (OdUInt32 i = 0; i < nPoints * 3; ++i) {
111 
112  for (int j = level - 1; j >= 0; --j) {
113  unsigned int val = byte >> bitShift & 1;
114  value += val << j;
115  mask >>=1;
116  bitShift--;
117 
118  if(!mask) {
119  mask = 0x80;
120  bitShift = 7;
121 
122  if (i == nPoints * 3 - 1 && j == 0)
123  continue;
124 
125  byte = origBuf.getByte();
126  bytesCnt++;
127  }
128  }
129 
130  float newVal = ((max[comp] - min[comp]) / pow(2.0, level)) * value + min[comp];
131  if (isLog)
132  printf("v:%f n:%d \t", newVal, value);
133 
134  value = 0;
135  comp = (comp >= 2) ? 0 : comp + 1;
136  OdPlatformStreamer::wrFloat(targetBuf, newVal);
137  }
138 
139  if (isLog)
140  printf("End compression\nBytes readed: %d\n", bytesCnt);
141 }
142 
143 }
144 }
unsigned int OdUInt32
unsigned char OdUInt8
const OdGePoint3d & maxPoint() const
Definition: GeExtents3d.h:259
const OdGePoint3d & minPoint() const
Definition: GeExtents3d.h:254
double z
Definition: GePoint3d.h:369
double y
Definition: GePoint3d.h:368
double x
Definition: GePoint3d.h:367
static void wrFloat(OdStreamBuf &out, float val)
static double rdDouble(OdStreamBuf &streamBuf)
virtual OdUInt8 getByte()
virtual void putByte(OdUInt8 value)
GLint level
Definition: gles2_ext.h:110
GLenum GLint GLuint mask
Definition: gles2_ext.h:262
GLsizei const GLfloat * value
Definition: gles2_ext.h:302
const T & min(const T &x, const T &y)
const T & max(const T &x, const T &y)
void decompress(const OdUInt32 &nPoints, const OdUInt8 &level, const OdGeExtents3d &ext, OdStreamBuf &origBuf, OdStreamBuf &targetBuf, const bool isLog=false)
Definition: LODAlgorithm.h:87
void compress(const OdUInt32 &nPoints, const OdUInt8 &level, const OdGeExtents3d &ext, OdStreamBuf &origBuf, OdStreamBuf &targetBuf, const bool isLog=false)
Definition: LODAlgorithm.h:31