Welcome to mirror list, hosted at ThFree Co, Russian Federation.

mesh_edgebreaker_encoder.cc « mesh « compression « draco « src « draco « draco « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5aff5d8cc10f9006d8aef91f4d0d67a80bbd4ec8 (plain)
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
// Copyright 2016 The Draco Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "draco/compression/mesh/mesh_edgebreaker_encoder.h"

#include "draco/compression/mesh/mesh_edgebreaker_encoder_impl.h"
#include "draco/compression/mesh/mesh_edgebreaker_traversal_predictive_encoder.h"
#include "draco/compression/mesh/mesh_edgebreaker_traversal_valence_encoder.h"

namespace draco {

MeshEdgebreakerEncoder::MeshEdgebreakerEncoder() {}

bool MeshEdgebreakerEncoder::InitializeEncoder() {
  const bool is_standard_edgebreaker_available =
      options()->IsFeatureSupported(features::kEdgebreaker);
  const bool is_predictive_edgebreaker_available =
      options()->IsFeatureSupported(features::kPredictiveEdgebreaker);

  impl_ = nullptr;
  // For tiny meshes it's usually better to use the basic edgebreaker as the
  // overhead of the predictive one may turn out to be too big.
  // TODO(b/111065939): Check if this can be improved.
  const bool is_tiny_mesh = mesh()->num_faces() < 1000;

  int selected_edgebreaker_method =
      options()->GetGlobalInt("edgebreaker_method", -1);
  if (selected_edgebreaker_method == -1) {
    if (is_standard_edgebreaker_available &&
        (options()->GetSpeed() >= 5 || !is_predictive_edgebreaker_available ||
         is_tiny_mesh)) {
      selected_edgebreaker_method = MESH_EDGEBREAKER_STANDARD_ENCODING;
    } else {
      selected_edgebreaker_method = MESH_EDGEBREAKER_VALENCE_ENCODING;
    }
  }

  if (selected_edgebreaker_method == MESH_EDGEBREAKER_STANDARD_ENCODING) {
    if (is_standard_edgebreaker_available) {
      buffer()->Encode(
          static_cast<uint8_t>(MESH_EDGEBREAKER_STANDARD_ENCODING));
      impl_ = std::unique_ptr<MeshEdgebreakerEncoderImplInterface>(
          new MeshEdgebreakerEncoderImpl<MeshEdgebreakerTraversalEncoder>());
    }
  } else if (selected_edgebreaker_method == MESH_EDGEBREAKER_VALENCE_ENCODING) {
    buffer()->Encode(static_cast<uint8_t>(MESH_EDGEBREAKER_VALENCE_ENCODING));
    impl_ = std::unique_ptr<MeshEdgebreakerEncoderImplInterface>(
        new MeshEdgebreakerEncoderImpl<
            MeshEdgebreakerTraversalValenceEncoder>());
  }
  if (!impl_) {
    return false;
  }
  if (!impl_->Init(this)) {
    return false;
  }
  return true;
}

bool MeshEdgebreakerEncoder::GenerateAttributesEncoder(int32_t att_id) {
  if (!impl_->GenerateAttributesEncoder(att_id)) {
    return false;
  }
  return true;
}

bool MeshEdgebreakerEncoder::EncodeAttributesEncoderIdentifier(
    int32_t att_encoder_id) {
  if (!impl_->EncodeAttributesEncoderIdentifier(att_encoder_id)) {
    return false;
  }
  return true;
}

Status MeshEdgebreakerEncoder::EncodeConnectivity() {
  return impl_->EncodeConnectivity();
}

void MeshEdgebreakerEncoder::ComputeNumberOfEncodedPoints() {
  if (!impl_) {
    return;
  }
  const CornerTable *const corner_table = impl_->GetCornerTable();
  if (!corner_table) {
    return;
  }
  size_t num_points =
      corner_table->num_vertices() - corner_table->NumIsolatedVertices();

  if (mesh()->num_attributes() > 1) {
    // Gather all corner tables for all non-position attributes.
    std::vector<const MeshAttributeCornerTable *> attribute_corner_tables;
    for (int i = 0; i < mesh()->num_attributes(); ++i) {
      if (mesh()->attribute(i)->attribute_type() ==
          GeometryAttribute::POSITION) {
        continue;
      }
      const MeshAttributeCornerTable *const att_corner_table =
          GetAttributeCornerTable(i);
      // Attribute corner table may not be used in some configurations. For
      // these cases we can assume the attribute connectivity to be the same as
      // the connectivity of the position data.
      if (att_corner_table) {
        attribute_corner_tables.push_back(att_corner_table);
      }
    }

    // Add a new point based on the configuration of interior attribute seams
    // (replicating what the decoder would do).
    for (VertexIndex vi(0); vi < corner_table->num_vertices(); ++vi) {
      if (corner_table->IsVertexIsolated(vi)) {
        continue;
      }
      // Go around all corners of the vertex and keep track of the observed
      // attribute seams.
      const CornerIndex first_corner_index = corner_table->LeftMostCorner(vi);
      const PointIndex first_point_index =
          mesh()->CornerToPointId(first_corner_index);

      PointIndex last_point_index = first_point_index;
      CornerIndex last_corner_index = first_corner_index;
      CornerIndex corner_index = corner_table->SwingRight(first_corner_index);
      size_t num_attribute_seams = 0;
      while (corner_index != kInvalidCornerIndex) {
        const PointIndex point_index = mesh()->CornerToPointId(corner_index);
        bool seam_found = false;
        if (point_index != last_point_index) {
          // Point index changed - new attribute seam detected.
          seam_found = true;
          last_point_index = point_index;
        } else {
          // Even though point indices matches, there still may be a seam caused
          // by non-manifold connectivity of non-position attribute data.
          for (int i = 0; i < attribute_corner_tables.size(); ++i) {
            if (attribute_corner_tables[i]->Vertex(corner_index) !=
                attribute_corner_tables[i]->Vertex(last_corner_index)) {
              seam_found = true;
              break;  // No need to process other attributes.
            }
          }
        }
        if (seam_found) {
          ++num_attribute_seams;
        }

        if (corner_index == first_corner_index) {
          break;
        }

        // Proceed to the next corner
        last_corner_index = corner_index;
        corner_index = corner_table->SwingRight(corner_index);
      }

      if (!corner_table->IsOnBoundary(vi) && num_attribute_seams > 0) {
        // If the last visited point index is the same as the first point index
        // we traveled all the way around the vertex. In this case the number of
        // new points should be num_attribute_seams - 1
        num_points += num_attribute_seams - 1;
      } else {
        // Else the vertex was either on a boundary (i.e. we couldn't travel all
        // around the vertex), or we ended up at a different point. In both of
        // these cases, the number of new points is equal to the number of
        // attribute seams.
        num_points += num_attribute_seams;
      }
    }
  }
  set_num_encoded_points(num_points);
}

void MeshEdgebreakerEncoder::ComputeNumberOfEncodedFaces() {
  if (!impl_) {
    return;
  }
  const CornerTable *const corner_table = impl_->GetCornerTable();
  if (!corner_table) {
    return;
  }
  set_num_encoded_faces(corner_table->num_faces() -
                        corner_table->NumDegeneratedFaces());
}

}  // namespace draco