MVE - Multi-View Environment mve-devel
Loading...
Searching...
No Matches
single_view.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015, Ronny Klowsky, Simon Fuhrmann
3 * TU Darmstadt - Graphics, Capture and Massively Parallel Computing
4 * All rights reserved.
5 *
6 * This software may be modified and distributed under the terms
7 * of the BSD 3-Clause license. See the LICENSE.txt file for details.
8 */
9
10#include <cassert>
11#include <limits>
12
13#include "util/file_system.h"
14#include "mve/image_io.h"
15#include "mve/image_tools.h"
16#include "mve/depthmap.h"
17#include "mve/mesh_io_ply.h"
18#include "mve/view.h"
19#include "dmrecon/defines.h"
20#include "dmrecon/single_view.h"
21
23
24SingleView::SingleView(mve::Scene::Ptr _scene,
25 mve::View::Ptr _view, std::string const& _embedding)
26 : scene(_scene)
27 , view(_view)
28 , embedding(_embedding)
29 , has_target_level(false)
30 , minLevel(std::numeric_limits<int>::max())
31{
32 /* Argument sanity checks. */
33 if (scene == nullptr)
34 throw std::invalid_argument("Null scene given");
35 if (view == nullptr || !view->is_camera_valid())
36 throw std::invalid_argument("Null view given");
37 if (embedding.empty())
38 throw std::invalid_argument("Empty embedding name");
39
40 /* Initialize camera for the view. */
41 mve::CameraInfo cam = view->get_camera();
42 cam.fill_camera_pos(*this->camPos);
43 cam.fill_world_to_cam(*this->worldToCam);
44
45 /* Initialize view source level (original image size). */
46 mve::View::ImageProxy const* proxy = view->get_image_proxy(_embedding);
47 if (proxy == nullptr)
48 throw std::invalid_argument("No color image found");
49 this->source_level = ImagePyramidLevel(cam, proxy->width, proxy->height);
50}
51
52SingleView::~SingleView()
53{
54 source_level.image.reset();
55 target_level.image.reset();
56 img_pyramid.reset();
57 ImagePyramidCache::cleanup();
58}
59
60void
61SingleView::loadColorImage(int _minLevel)
62{
63 minLevel = _minLevel;
64 img_pyramid = ImagePyramidCache::get(this->scene, this->view, this->embedding, minLevel);
65}
66
67void
68SingleView::prepareMasterView(int scale)
69{
70 /* Prepare target level (view is the master view). */
71 this->target_level = (*this->img_pyramid)[scale];
72 this->has_target_level = true;
73 this->createFileName(scale);
74
75 /* Create images for reconstruction. */
76 int const scaled_width = this->target_level.width;
77 int const scaled_height = this->target_level.height;
78 this->depthImg = mve::FloatImage::create(scaled_width, scaled_height, 1);
79 this->normalImg = mve::FloatImage::create(scaled_width, scaled_height, 3);
80 this->dzImg = mve::FloatImage::create(scaled_width, scaled_height, 2);
81 this->confImg = mve::FloatImage::create(scaled_width, scaled_height, 1);
82}
83
85SingleView::viewRay(int x, int y, int level) const
86{
87 return this->viewRay(float(x), float(y), level);
88}
89
91SingleView::viewRay(float x, float y, int level) const
92{
93 math::Vec3f ray = mve::geom::pixel_3dpos(x, y, 1.f, this->img_pyramid->at(level).invproj);
94 math::Matrix3f rot(view->get_camera().rot);
95 return rot.transposed() * ray;
96}
97
99SingleView::viewRayScaled(int x, int y) const
100{
101 assert(this->has_target_level);
102
103 math::Vec3f ray = mve::geom::pixel_3dpos(x, y, 1.f, this->target_level.invproj);
104 math::Matrix3f rot(view->get_camera().rot);
105 return rot.transposed() * ray;
106}
107
108bool
109SingleView::pointInFrustum(math::Vec3f const& wp) const
110{
111 math::Vec3f cp = this->worldToCam.mult(wp, 1.0f);
112 // check whether point lies in front of camera
113 if (cp[2] <= 0.0f)
114 return false;
115 math::Vec3f sp = this->source_level.proj * cp;
116 float x = sp[0] / sp[2] - 0.5f;
117 float y = sp[1] / sp[2] - 0.5f;
118 return x >= 0 && x <= this->source_level.width - 1
119 && y >= 0 && y <= this->source_level.height - 1;
120}
121
122void
123SingleView::saveReconAsPly(std::string const& path, float scale) const
124{
125 if (path.empty()) {
126 throw std::invalid_argument("Empty path");
127 }
128 if (!util::fs::dir_exists(path.c_str()))
129 util::fs::mkdir(path.c_str());
130
131 std::string name(this->createFileName(scale));
132 std::string plyname = util::fs::join_path(path, name + ".ply");
133 std::string xfname = util::fs::join_path(path, name + ".xf");
134
135 mve::geom::save_ply_view(plyname, view->get_camera(),
136 this->depthImg, this->confImg, this->target_level.image);
137 mve::geom::save_xf_file(xfname, view->get_camera());
138}
139
Matrix class for arbitrary dimensions and types.
Definition matrix.h:54
Matrix< T, M, N > transposed(void) const
Returns a transposed copy of self by treating rows as columns.
Definition matrix.h:448
Vector class for arbitrary dimensions and types.
Definition vector.h:87
static Ptr create(void)
Smart pointer image constructor.
Definition image.h:191
std::shared_ptr< Scene > Ptr
Definition scene.h:37
std::shared_ptr< View > Ptr
Definition view.h:68
#define MVS_NAMESPACE_BEGIN
Definition defines.h:18
#define MVS_NAMESPACE_END
Definition defines.h:19
T const & max(T const &a, T const &b, T const &c)
Returns the maximum value of three arguments.
Definition functions.h:227
math::Vec3f pixel_3dpos(int64_t x, int64_t y, float depth, math::Matrix3f const &invproj)
Function that calculates the pixel 3D position in camera coordinates for pixel (x,...
Definition depthmap.cc:150
void save_ply_view(std::string const &filename, CameraInfo const &camera, FloatImage::ConstPtr depth_map, FloatImage::ConstPtr confidence_map, ByteImage::ConstPtr color_image)
Stores a scanalize-compatible PLY file from a depth map.
void save_xf_file(std::string const &filename, CameraInfo const &camera)
Stores a scanalyze compatible XF file with camera transformation from camera to world coordinates.
STL namespace.
bool mkdir(char const *pathname)
Creates a new directory.
bool dir_exists(char const *pathname)
Determines if the given path is a directory.
std::string join_path(std::string const &path1, std::string const &path2)
Concatenate and canonicalize two paths.
Per-view camera information with various helper functions.
Definition camera.h:24
void fill_world_to_cam(float *mat) const
Stores world to camera 4x4 matrix in array pointed to by mat.
Definition camera.cc:61
void fill_camera_pos(float *pos) const
Stores camera position 3-vector into array pointed to by pos.
Definition camera.cc:34
Proxy for images.
Definition view.h:87