[7d146a] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
| 4 | * Copyright (C) 2015 Frederik Heber. All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | *
|
---|
| 7 | * This file is part of MoleCuilder.
|
---|
| 8 | *
|
---|
| 9 | * MoleCuilder is free software: you can redistribute it and/or modify
|
---|
| 10 | * it under the terms of the GNU General Public License as published by
|
---|
| 11 | * the Free Software Foundation, either version 2 of the License, or
|
---|
| 12 | * (at your option) any later version.
|
---|
| 13 | *
|
---|
| 14 | * MoleCuilder is distributed in the hope that it will be useful,
|
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 17 | * GNU General Public License for more details.
|
---|
| 18 | *
|
---|
| 19 | * You should have received a copy of the GNU General Public License
|
---|
| 20 | * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 21 | */
|
---|
| 22 |
|
---|
| 23 | /*
|
---|
| 24 | * TriangleMatcher.cpp
|
---|
| 25 | *
|
---|
[c66ae6] | 26 | * Created on: Jun 15, 2015
|
---|
[7d146a] | 27 | * Author: heber
|
---|
| 28 | *
|
---|
| 29 | *
|
---|
| 30 | * This is a small study in C++11 that compares at least two
|
---|
| 31 | * files containing the triangle index triples of tesselations
|
---|
| 32 | * for equivalence
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | // include config.h
|
---|
| 36 | #ifdef HAVE_CONFIG_H
|
---|
| 37 | #include <config.h>
|
---|
| 38 | #endif
|
---|
| 39 |
|
---|
| 40 | #include "CodePatterns/MemDebug.hpp"
|
---|
| 41 |
|
---|
| 42 | #include <boost/exception/all.hpp>
|
---|
| 43 | #include <boost/filesystem/operations.hpp>
|
---|
| 44 | #include <boost/filesystem/path.hpp>
|
---|
| 45 | #include <boost/tokenizer.hpp>
|
---|
| 46 |
|
---|
| 47 | #include <algorithm>
|
---|
| 48 | #include <fstream>
|
---|
| 49 | #include <iostream>
|
---|
| 50 | #include <iterator>
|
---|
| 51 | #include <sstream>
|
---|
| 52 | #include <string>
|
---|
| 53 | #include <map>
|
---|
| 54 |
|
---|
| 55 | #include "types.hpp"
|
---|
| 56 |
|
---|
| 57 | using namespace detail;
|
---|
| 58 |
|
---|
| 59 | namespace bf = boost::filesystem;
|
---|
| 60 |
|
---|
| 61 | typedef boost::error_info<
|
---|
| 62 | struct tag_IllegalTesselationFileContent_LineNumber,
|
---|
| 63 | int> IllegalTesselationFileContent_LineNumber;
|
---|
| 64 |
|
---|
| 65 | typedef boost::error_info<
|
---|
| 66 | struct tag_IllegalTesselationFileContent_Entries,
|
---|
| 67 | int> IllegalTesselationFileContent_Entries;
|
---|
| 68 |
|
---|
| 69 | typedef boost::error_info<
|
---|
| 70 | struct tag_IllegalTesselationFileContent_Line,
|
---|
| 71 | int> IllegalTesselationFileContent_Line;
|
---|
| 72 |
|
---|
| 73 | //!> Exception for illegal lines encountered in tesselation files
|
---|
| 74 | struct IllegalTesselationFileContentException:
|
---|
| 75 | virtual boost::exception, virtual std::exception { };
|
---|
| 76 |
|
---|
| 77 | Triangle::index_t toIndex(const std::string &_element)
|
---|
| 78 | {
|
---|
| 79 | Triangle::index_t index;
|
---|
| 80 | std::stringstream tokenstring(_element);
|
---|
| 81 | tokenstring >> index;
|
---|
| 82 | return index;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | template <class T>
|
---|
| 86 | std::ostream& operator<<(
|
---|
| 87 | std::ostream &_ost,
|
---|
| 88 | const std::set<T> &elements)
|
---|
| 89 | {
|
---|
| 90 | for( const T elem : elements)
|
---|
| 91 | _ost << elem << std::endl;
|
---|
| 92 | return _ost;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | /** Parses a tesselation file into a vector of Triangles.
|
---|
| 96 | *
|
---|
| 97 | * \param _filename name of file to parse
|
---|
| 98 | * \return tesselation_t structure filled with triangle index triples
|
---|
| 99 | */
|
---|
| 100 | const tesselation_t parseFile(const boost::filesystem::path &_filename)
|
---|
| 101 | {
|
---|
| 102 | tesselation_t tesselation;
|
---|
| 103 |
|
---|
| 104 | std::ifstream infile(_filename.string().c_str());
|
---|
| 105 | boost::char_separator<char> sep(" \t");
|
---|
| 106 |
|
---|
| 107 | typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
|
---|
| 108 | std::string line;
|
---|
| 109 | int line_number = 1;
|
---|
| 110 | // read up till encountering an empty line
|
---|
| 111 | while (!infile.eof()) {
|
---|
| 112 | std::getline(infile, line);
|
---|
| 113 | if (line.empty())
|
---|
| 114 | break;
|
---|
| 115 | }
|
---|
| 116 | while (!infile.eof()) {
|
---|
| 117 | std::getline(infile, line);
|
---|
| 118 | tokenizer tokens(line, sep);
|
---|
| 119 | Triangle::indices_t indices;
|
---|
| 120 | const int entries = std::distance( tokens.begin(), tokens.end() );
|
---|
| 121 | // check for empty lines
|
---|
| 122 | if (entries == 0)
|
---|
| 123 | continue;
|
---|
| 124 | // entries must be exactly three if any
|
---|
| 125 | if ( entries != NUMBERINDICESTRIANGLE )
|
---|
| 126 | throw IllegalTesselationFileContentException()
|
---|
| 127 | << IllegalTesselationFileContent_LineNumber(line_number)
|
---|
| 128 | << IllegalTesselationFileContent_Entries(entries);
|
---|
| 129 | std::transform(
|
---|
| 130 | tokens.begin(),
|
---|
| 131 | tokens.end(),
|
---|
| 132 | indices.begin(),
|
---|
| 133 | toIndex);
|
---|
| 134 | tesselation.insert(Triangle(indices));
|
---|
| 135 |
|
---|
| 136 | ++line_number;
|
---|
| 137 | }
|
---|
| 138 | infile.close();
|
---|
| 139 |
|
---|
| 140 | return tesselation;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | int main(int argc, char **argv) {
|
---|
| 144 |
|
---|
| 145 | /// check arguments and give usage
|
---|
| 146 | if (argc <= 2) {
|
---|
| 147 | std::cerr << "Usage: " << argv[0] << " <list of files>\n";
|
---|
| 148 | std::cerr << "At least two tesselation files need to be specified that contain\n";
|
---|
| 149 | std::cerr << "each three indices per line.\n";
|
---|
| 150 |
|
---|
| 151 | return 255;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | /// read in files and parse into structures
|
---|
| 155 | typedef std::multimap<tesselation_t, std::string> tesselations_filename_t;
|
---|
| 156 | //!> contains all tesselations parsed from files
|
---|
| 157 | tesselations_filename_t tesselations;
|
---|
| 158 | for (int i=1; i<argc;++i) {
|
---|
| 159 | const bf::path filename(argv[i]);
|
---|
| 160 | if (bf::exists(filename)) {
|
---|
| 161 | try {
|
---|
| 162 | tesselation_t tesselation = parseFile(filename);
|
---|
| 163 | tesselations.insert(
|
---|
| 164 | std::make_pair(tesselation, filename.string()) );
|
---|
| 165 | } catch (IllegalTesselationFileContentException &e) {
|
---|
| 166 | std::cerr << "Illegal line at "
|
---|
| 167 | << *boost::get_error_info<IllegalTesselationFileContent_LineNumber>(e)
|
---|
| 168 | << " encountered with "
|
---|
| 169 | << *boost::get_error_info<IllegalTesselationFileContent_Entries>(e)
|
---|
| 170 | << " entries in "
|
---|
| 171 | << filename.string()
|
---|
| 172 | << ", aborting.\n";
|
---|
| 173 | return 5;
|
---|
| 174 | }
|
---|
| 175 | } else {
|
---|
| 176 | std::cerr << "The following file does not exist "
|
---|
| 177 | << filename.string() << ", aborting.\n";
|
---|
| 178 | return 5;
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | /// make comparison for equivalence
|
---|
| 183 | assert( tesselations.size() >= 2 );
|
---|
| 184 | tesselations_filename_t::const_iterator advancer = tesselations.begin();
|
---|
| 185 | tesselations_filename_t::const_iterator iter = advancer++;
|
---|
| 186 |
|
---|
| 187 | bool AllEqual = true;
|
---|
| 188 | while( advancer != tesselations.end() ) {
|
---|
| 189 | if (advancer->first == iter->first) {
|
---|
| 190 | std::cout << "MATCH: "
|
---|
| 191 | << advancer->second << " and " << iter->second
|
---|
| 192 | << std::endl;
|
---|
| 193 | } else {
|
---|
| 194 | std::cout << advancer->first << " and " << iter->first
|
---|
| 195 | << " don't match.\n";
|
---|
| 196 | AllEqual = false;
|
---|
| 197 | }
|
---|
| 198 | iter = advancer++;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | // exit (convert bool to int)
|
---|
| 202 | return (AllEqual ? 0 : 1);
|
---|
| 203 | }
|
---|