ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
normal_map.cc
Go to the documentation of this file.
1 // Copyright (c) 2018, ETH Zurich and UNC Chapel Hill.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 //
14 // * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of
15 // its contributors may be used to endorse or promote products derived
16 // from this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Johannes L. Schoenberger (jsch-at-demuc-dot-de)
31 
32 #include "mvs/normal_map.h"
33 
34 #include "base/warp.h"
35 
36 namespace colmap {
37 namespace mvs {
38 
39 NormalMap::NormalMap() : Mat<float>(0, 0, 3) {}
40 
41 NormalMap::NormalMap(const size_t width, const size_t height)
42  : Mat<float>(width, height, 3) {}
43 
45  : Mat<float>(mat.GetWidth(), mat.GetHeight(), mat.GetDepth()) {
46  CHECK_EQ(mat.GetDepth(), 3);
47  data_ = mat.GetData();
48 }
49 
50 void NormalMap::Rescale(const float factor) {
51  if (width_ * height_ == 0) {
52  return;
53  }
54 
55  const size_t new_width = std::round(width_ * factor);
56  const size_t new_height = std::round(height_ * factor);
57  std::vector<float> new_data(new_width * new_height * 3);
58 
59  // Resample the normal map.
60  for (size_t d = 0; d < 3; ++d) {
61  const size_t offset = d * width_ * height_;
62  const size_t new_offset = d * new_width * new_height;
63  DownsampleImage(data_.data() + offset, height_, width_, new_height,
64  new_width, new_data.data() + new_offset);
65  }
66 
67  data_ = new_data;
68  width_ = new_width;
69  height_ = new_height;
70 
71  data_.shrink_to_fit();
72 
73  // Re-normalize the normal vectors.
74  for (size_t r = 0; r < height_; ++r) {
75  for (size_t c = 0; c < width_; ++c) {
76  Eigen::Vector3f normal(Get(r, c, 0), Get(r, c, 1), Get(r, c, 2));
77  const float squared_norm = normal.squaredNorm();
78  if (squared_norm > 0) {
79  normal /= std::sqrt(squared_norm);
80  }
81  Set(r, c, 0, normal(0));
82  Set(r, c, 1, normal(1));
83  Set(r, c, 2, normal(2));
84  }
85  }
86 }
87 
88 void NormalMap::Downsize(const size_t max_width, const size_t max_height) {
89  if (height_ <= max_height && width_ <= max_width) {
90  return;
91  }
92  const float factor_x = static_cast<float>(max_width) / width_;
93  const float factor_y = static_cast<float>(max_height) / height_;
94  Rescale(std::min(factor_x, factor_y));
95 }
96 
98  CHECK_GT(width_, 0);
99  CHECK_GT(height_, 0);
100  CHECK_EQ(depth_, 3);
101 
102  Bitmap bitmap;
103  bitmap.Allocate(width_, height_, true);
104 
105  for (size_t y = 0; y < height_; ++y) {
106  for (size_t x = 0; x < width_; ++x) {
107  float normal[3];
108  GetSlice(y, x, normal);
109  if (normal[0] != 0 || normal[1] != 0 || normal[2] != 0) {
110  const BitmapColor<float> color(127.5f * (-normal[0] + 1),
111  127.5f * (-normal[1] + 1),
112  -255.0f * normal[2]);
113  bitmap.SetPixel(x, y, color.Cast<uint8_t>());
114  } else {
115  bitmap.SetPixel(x, y, BitmapColor<uint8_t>(0));
116  }
117  }
118  }
119 
120  return bitmap;
121 }
122 
123 } // namespace mvs
124 } // namespace colmap
double normal[3]
int width
int height
int offset
math::float4 color
bool SetPixel(const int x, const int y, const BitmapColor< uint8_t > &color)
Definition: bitmap.cc:199
bool Allocate(const int width, const int height, const bool as_rgb)
Definition: bitmap.cc:104
size_t GetDepth() const
Definition: mat.h:81
std::vector< float > data_
Definition: mat.h:54
void Set(const size_t row, const size_t col, const float value)
Definition: mat.h:118
float Get(const size_t row, const size_t col, const size_t slice=0) const
Definition: mat.h:91
void GetSlice(const size_t row, const size_t col, float *values) const
Definition: mat.h:96
const std::vector< T > & GetData() const
Definition: mat.h:113
void Rescale(const float factor)
Definition: normal_map.cc:50
void Downsize(const size_t max_width, const size_t max_height)
Definition: normal_map.cc:88
Bitmap ToBitmap() const
Definition: normal_map.cc:97
normal_z y
float
normal_z x
void DownsampleImage(const float *data, const int rows, const int cols, const int new_rows, const int new_cols, float *downsampled)
Definition: warp.cc:226