Lagrange
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Modules Pages
save_graph.h
1/*
2 * Copyright 2020 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13
14#include <string>
15
16#include <Eigen/Dense>
17
18#include <lagrange/fs/filesystem.h>
19
20namespace lagrange {
21
32template <typename DerivedV, typename DerivedE>
34 const fs::path &filename,
35 const Eigen::MatrixBase<DerivedV> &V,
36 const Eigen::MatrixBase<DerivedE> &E)
37{
38 using namespace Eigen;
39 fs::ofstream out(filename);
40 if (!out.is_open()) {
41 throw std::runtime_error("failed to open file " + filename.string());
42 }
43 Eigen::MatrixXd VV = V.template cast<double>();
44 VV.conservativeResize(V.rows(), 3);
45 VV.col(2).setZero();
46 out << "OFF\n"
47 << VV.rows() << " " << E.rows() << " 0\n"
48 << VV.format(IOFormat(FullPrecision, DontAlignCols, " ", "\n", "", "", "", "\n"))
49 << (E.array()).format(
50 IOFormat(FullPrecision, DontAlignCols, " ", "\n", "2 ", "", "", "\n"));
51}
52
63template <typename DerivedV, typename DerivedE>
65 const fs::path &filename,
66 const Eigen::MatrixBase<DerivedV> &V,
67 const Eigen::MatrixBase<DerivedE> &E)
68{
69 if (filename.extension() == ".off") {
70 save_graph_off(filename, V, E);
71 } else {
72 throw std::runtime_error("Unsupported file extension");
73 }
74}
75
76} // namespace lagrange
Main namespace for Lagrange.
Definition: AABBIGL.h:30
void save_graph(const fs::path &filename, const Eigen::MatrixBase< DerivedV > &V, const Eigen::MatrixBase< DerivedE > &E)
Saves a 2D undirected graph (V,E) based on filename extension.
Definition: save_graph.h:64
void save_graph_off(const fs::path &filename, const Eigen::MatrixBase< DerivedV > &V, const Eigen::MatrixBase< DerivedE > &E)
Saves a 2D undirected graph (V,E) using off format.
Definition: save_graph.h:33