Commit 0b15ba1b authored by karius's avatar karius

added chunked rotation matrix iterator

parent bd3cdcb3
/*
* CRotationGrid.cpp
*
* Created on: Sep 17, 2019
* Author: kkarius
*/
#include <CRotationGrid.h>
CRotationGrid::CRotationGrid(std::string rot_file_path, size_t chunksize) : // @suppress("Class members should be properly initialized")
m_filepath(rot_file_path), m_rotation_format(ERotationFormat::eMatrix), m_chunksize(
chunksize) {
try {
m_filestream.open(m_filepath, std::fstream::in);
} catch (std::exception& e) {
throw e;
}
//also parses total number of rotations and sanity checks chunk size
m_beginning_found = x_find_beginning();
if (m_beginning_found && m_rotation_format == ERotationFormat::eMatrix) {
m_matrix_chunk = (float *) malloc(9 * chunksize * sizeof(float));
//get first batch into m_matrix_chunk
(*this)++;
}
}
CRotationGrid& CRotationGrid::operator++(int inc) {
if (m_beginning_found) {
for (int i = 0; i < m_chunksize; i++) {
if (!m_filestream.eof()) {
if (x_parse_line()) {
if (m_rotation_format == ERotationFormat::eMatrix) {
size_t offset = 0;
for (int j = 0; j < 9; j++) {
m_matrix_chunk[9 * i + j] = m_current_matrix[j];
}
}
}
}
}
}
return *this;
}
float *& CRotationGrid::operator *() {
return m_matrix_chunk;
}
bool CRotationGrid::operator!() {
return !m_filestream.eof();
}
bool CRotationGrid::x_find_beginning() {
bool success = false;
std::vector<std::string> strs;
while (!m_filestream.eof()) {
std::getline(m_filestream, m_current_line);
if (m_current_line.find("format") != std::string::npos) {
//skip parameter m_current_lines
std::getline(m_filestream, m_current_line);
std::getline(m_filestream, m_current_line);
boost::split(strs, m_current_line, boost::is_any_of(" \t"),
boost::token_compress_on);
rotation_num = std::stoi(strs[0]);
covering_radius = std::stof(strs[1]);
success = true & m_chunksize <= MAX_CHUNKSIZE;
break;
}
}
return success;
}
bool CRotationGrid::x_quat_to_rot_mat() {
bool success = false;
if (m_current_quat.x * m_current_quat.x
+ m_current_quat.y * m_current_quat.y
+ m_current_quat.z * m_current_quat.z
+ m_current_quat.w * m_current_quat.w - 1.0 < 1.0e-5) {
success = true;
}
// 0 1 2
// 3 4 5 --> 0 1 2 3 4 5 6 7 8
// 6 7 8
m_current_matrix[0] = 1 - 2 * m_current_quat.z * m_current_quat.z
- 2 * m_current_quat.w * m_current_quat.w;
m_current_matrix[1] = 2 * m_current_quat.y * m_current_quat.z
- 2 * m_current_quat.x * m_current_quat.w;
m_current_matrix[2] = 2 * m_current_quat.y * m_current_quat.w
+ 2 * m_current_quat.x * m_current_quat.z;
m_current_matrix[3] = 2 * m_current_quat.z * m_current_quat.y
+ 2 * m_current_quat.x * m_current_quat.w;
m_current_matrix[4] = 1 - 2 * m_current_quat.w * m_current_quat.w
- 2 * m_current_quat.y * m_current_quat.y;
m_current_matrix[5] = 2 * m_current_quat.z * m_current_quat.w
- 2 * m_current_quat.x * m_current_quat.y;
m_current_matrix[6] = 2 * m_current_quat.w * m_current_quat.y
- 2 * m_current_quat.x * m_current_quat.z;
m_current_matrix[7] = 2 * m_current_quat.w * m_current_quat.z
+ 2 * m_current_quat.x * m_current_quat.y;
m_current_matrix[8] = 1 - 2 * m_current_quat.y * m_current_quat.y
- 2 * m_current_quat.z * m_current_quat.z;
return success;
}
bool CRotationGrid::x_parse_line() {
bool success = true;
std::vector<std::string> strs;
std::getline(m_filestream, m_current_line);
boost::trim(m_current_line);
if (m_current_line.size() != 0) {
boost::split(strs, m_current_line, boost::is_any_of(" \t"),
boost::token_compress_on);
if (strs.size() >= 4) {
m_current_quat.x = std::stof(strs[0]);
m_current_quat.y = std::stof(strs[1]);
m_current_quat.z = std::stof(strs[2]);
m_current_quat.w = std::stof(strs[3]);
}
if (m_rotation_format == ERotationFormat::eMatrix) {
success &= x_quat_to_rot_mat();
}
}
return success;
}
CRotationGrid::~CRotationGrid() {
// TODO Auto-generated destructor stub
}
/*
* CRotationGrid.h
*
* Created on: Sep 17, 2019
* Author: kkarius
*/
#ifndef CROTATIONGRID_H_
#define CROTATIONGRID_H_
#include <cuda_util_include/cutil_math.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <boost/algorithm/string.hpp>
#include <fstream>
#define MAX_CHUNKSIZE 1024
enum ERotationFormat {
eMatrix
};
class CRotationGrid {
public:
CRotationGrid(std::string rot_file_path, size_t chunksize);
virtual ~CRotationGrid();
size_t rotation_num;
float covering_radius;
CRotationGrid& operator++(int);
float *&operator*();
bool operator!();
private:
size_t m_chunksize;
ERotationFormat m_rotation_format;
std::string m_filepath;
std::ifstream m_filestream;
std::string m_current_line;
bool m_beginning_found;
float * m_matrix_chunk;
float m_current_matrix[9];
float4 m_current_quat;
bool x_find_beginning(void);
bool x_parse_line(void);
bool x_quat_to_rot_mat();
};
#endif /* CROTATIONGRID_H_ */
This diff is collapsed.
......@@ -12,10 +12,12 @@ IMP_LINKS = -L/data/git/imp/build/lib/ -lRMF -limp_core -limp_rmf -limp_kernel -
LFLAGS=-L. $(IMP_LINKS) $(BOOST_LINKS)
all: CudaTest.cu CMrcReader.o
$(NVCC) $(LFLAGS) $(CFLAGS) -o CudaTest CudaTest.cu CMrcReader.o
all: CudaTest.cu CMrcReader.o CRotationGrid.o
$(NVCC) $(LFLAGS) $(CFLAGS) -o CudaTest CudaTest.cu CMrcReader.o CRotationGrid.o
CMrcReader.o: CMrcReader.cpp
$(NVCC) -c -o $@ $< $(CFLAGS)
CRotationGrid.o: CRotationGrid.cpp
$(NVCC) -c -o $@ $< $(CFLAGS)
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment