1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//
// svg.rs
// Copyright (C) 2019 Malcolm Ramsay <malramsay64@gmail.com>
// Distributed under terms of the MIT license.
//

use nalgebra::Matrix3;
use svg::node::element;
use svg::Document;

use crate::state::PackedState;
use crate::traits::*;
use crate::*;

impl ToSVG for Line2 {
    type Value = element::Path;

    fn as_svg(&self) -> Self::Value {
        let data = element::path::Data::new()
            .move_to((self.start.x, self.start.y))
            .line_by((self.end.x, self.end.y));
        element::Path::new().set("d", data)
    }
}

impl ToSVG for LJ2 {
    type Value = element::Circle;

    fn as_svg(&self) -> Self::Value {
        element::Circle::new()
            .set("r", self.sigma / 2.)
            .set("cx", self.position.x)
            .set("cy", self.position.y)
    }
}

impl ToSVG for LJShape2 {
    type Value = element::Group;

    fn as_svg(&self) -> Self::Value {
        let mut smol = element::Group::new();
        for item in self {
            smol = smol.add(item.as_svg())
        }
        smol
    }
}

impl ToSVG for LineShape {
    type Value = element::Group;

    fn as_svg(&self) -> Self::Value {
        let start = self.items[0].start;
        let mut data = element::path::Data::new().move_to((start.x, start.y));

        for item in self {
            data = data.line_to((item.end.x, item.end.y));
        }
        element::Group::new().add(element::Path::new().set("d", data.close()))
    }
}

impl ToSVG for Cell2 {
    type Value = element::Group;

    fn as_svg(&self) -> Self::Value {
        let corners = self.get_corners();
        let cell_data = element::path::Data::new()
            .move_to((corners[0].x, corners[0].y))
            .line_to((corners[1].x, corners[1].y))
            .line_to((corners[2].x, corners[2].y))
            .line_to((corners[3].x, corners[3].y))
            .close();

        element::Group::new().add(
            element::Path::new()
                .set("fill", "None")
                .set("stroke", "grey")
                .set("stroke-width", 0.1)
                .set("d", cell_data),
        )
    }
}

impl ToSVG for Atom2 {
    type Value = element::Circle;

    fn as_svg(&self) -> Self::Value {
        element::Circle::new()
            .set("r", self.radius)
            .set("cx", self.position.x)
            .set("cy", self.position.y)
    }
}

impl ToSVG for MolecularShape2 {
    type Value = element::Group;

    fn as_svg(&self) -> Self::Value {
        let mut smol = element::Group::new();
        for item in self {
            smol = smol.add(item.as_svg())
        }
        smol
    }
}

impl ToSVG for Transform2 {
    type Value = element::Use;

    fn as_svg(&self) -> Self::Value {
        let matrix: Matrix3<f64> = self.clone().into();
        element::Use::new().set(
            "transform",
            format!(
                "matrix({0} {1} {2} {3} {4} {5})",
                matrix[(0, 0)],
                matrix[(1, 0)],
                matrix[(0, 1)],
                matrix[(1, 1)],
                matrix[(0, 2)],
                matrix[(1, 2)],
            ),
        )
    }
}

impl<S> ToSVG for PotentialState<S>
where
    S: Shape + Potential,
{
    type Value = Document;

    fn as_svg(&self) -> Self::Value {
        let padding = self.shape.enclosing_radius();
        let viewbox =
            self.cell
                .get_corners()
                .iter()
                .map(|p| p * 3.)
                .fold((0., 0., 0., 0.), |acc, p| {
                    (
                        f64::min(p.x - padding, acc.0),
                        f64::min(p.y - padding, acc.1),
                        f64::max(2. * (p.x + padding), acc.2),
                        f64::max(2. * (p.y + padding), acc.3),
                    )
                });
        let mut doc = Document::new().set("viewBox", viewbox).add(
            element::Definitions::new()
                .add(self.cell.as_svg().set("id", "cell"))
                .add(self.shape.as_svg().set("id", "mol")),
        );
        for transform in self.cell.periodic_images(Transform2::identity(), 1, true) {
            doc = doc.add(transform.as_svg().set("href", "#cell"));
        }

        for position in self.relative_positions() {
            let transform = self.cell.to_cartesian_isometry(position);
            doc = doc.add(transform.as_svg().set("href", "#mol").set("fill", "blue"));
            for periodic in self.cell.periodic_images(position, 1, false) {
                doc = doc.add(periodic.as_svg().set("href", "#mol").set("fill", "green"));
            }
        }
        doc
    }
}

impl<S> ToSVG for PackedState<S>
where
    S: Shape + Intersect,
{
    type Value = Document;

    fn as_svg(&self) -> Self::Value {
        let padding = self.shape.enclosing_radius();
        let viewbox =
            self.cell
                .get_corners()
                .iter()
                .map(|p| p * 3.)
                .fold((0., 0., 0., 0.), |acc, p| {
                    (
                        f64::min(p.x - padding, acc.0),
                        f64::min(p.y - padding, acc.1),
                        f64::max(2. * (p.x + padding), acc.2),
                        f64::max(2. * (p.y + padding), acc.3),
                    )
                });
        let mut doc = Document::new().set("viewBox", viewbox).add(
            element::Definitions::new()
                .add(self.cell.as_svg().set("id", "cell"))
                .add(self.shape.as_svg().set("id", "mol")),
        );
        for transform in self.cell.periodic_images(Transform2::identity(), 1, true) {
            doc = doc.add(transform.as_svg().set("href", "#cell"));
        }
        for position in self.relative_positions() {
            let matrix = self.cell.to_cartesian_isometry(position);
            doc = doc.add(matrix.as_svg().set("href", "#mol").set("fill", "blue"));
            for transform in self.cell.periodic_images(position, 1, false) {
                doc = doc.add(transform.as_svg().set("href", "#mol").set("fill", "green"));
            }
        }
        doc
    }
}