Skip to content

Commit 8492241

Browse files
added 3d example quiver and fixed some stuff
1 parent dcd56a0 commit 8492241

File tree

3 files changed

+177
-5
lines changed

3 files changed

+177
-5
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#include <array>
2+
#include <cmath>
3+
#include <matplot/matplot.h>
4+
#include <vector>
5+
6+
std::vector<std::array<double, 3>> get_position();
7+
std::vector<std::array<double, 3>>
8+
get_vectors(const std::vector<std::array<double, 3>> &positions);
9+
10+
int main() {
11+
using namespace matplot;
12+
13+
auto positions = get_position();
14+
auto vectors = get_vectors(positions);
15+
vector_1d x;
16+
vector_1d y;
17+
vector_1d z;
18+
vector_1d u;
19+
vector_1d v;
20+
vector_1d w;
21+
vector_1d m;
22+
for (const auto &p : positions) {
23+
x.emplace_back(p[0]);
24+
y.emplace_back(p[1]);
25+
z.emplace_back(p[2]);
26+
}
27+
for (const auto &vec : vectors) {
28+
u.emplace_back(vec[0]);
29+
v.emplace_back(vec[1]);
30+
w.emplace_back(vec[2]);
31+
m.emplace_back(
32+
sqrt(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]));
33+
}
34+
35+
quiver3(x, y, z, u, v, w, m, 0.3)->normalize(true).line_width(2);
36+
37+
show();
38+
return 0;
39+
}
40+
41+
std::vector<std::array<double, 3>>
42+
get_vectors(const std::vector<std::array<double, 3>> &positions) {
43+
std::vector<std::array<double, 3>> vectors;
44+
for (const auto &pos : positions) {
45+
std::array<double, 3> result{0, 0, 0};
46+
result[0] =
47+
pos[0] * exp(-pow(pos[0], 2) - pow(pos[1], 2) - pow(pos[2], 2));
48+
result[1] =
49+
pos[1] * exp(-pow(pos[0], 2) - pow(pos[1], 2) - pow(pos[2], 2));
50+
result[2] =
51+
pos[2] * exp(-pow(pos[0], 2) - pow(pos[1], 2) - pow(pos[2], 2));
52+
vectors.emplace_back(result);
53+
}
54+
return vectors;
55+
}
56+
57+
std::vector<std::array<double, 3>> get_position() {
58+
std::vector<std::array<double, 3>> p = {
59+
{-1.0, -1.0, -1.0}, {-1.0, -1.0, -0.5}, {-1.0, -1.0, 0.0},
60+
{-1.0, -1.0, 0.5}, {-1.0, -1.0, 1.0}, {-1.0, -0.5, -1.0},
61+
{-1.0, -0.5, -0.5}, {-1.0, -0.5, 0.0}, {-1.0, -0.5, 0.5},
62+
{-1.0, -0.5, 1.0}, {-1.0, 0.0, -1.0}, {-1.0, 0.0, -0.5},
63+
{-1.0, 0.0, 0.0}, {-1.0, 0.0, 0.5}, {-1.0, 0.0, 1.0},
64+
{-1.0, 0.5, -1.0}, {-1.0, 0.5, -0.5}, {-1.0, 0.5, 0.0},
65+
{-1.0, 0.5, 0.5}, {-1.0, 0.5, 1.0}, {-1.0, 1.0, -1.0},
66+
{-1.0, 1.0, -0.5}, {-1.0, 1.0, 0.0}, {-1.0, 1.0, 0.5},
67+
{-1.0, 1.0, 1.0}, {-0.5, -1.0, -1.0}, {-0.5, -1.0, -0.5},
68+
{-0.5, -1.0, 0.0}, {-0.5, -1.0, 0.5}, {-0.5, -1.0, 1.0},
69+
{-0.5, -0.5, -1.0}, {-0.5, -0.5, -0.5}, {-0.5, -0.5, 0.0},
70+
{-0.5, -0.5, 0.5}, {-0.5, -0.5, 1.0}, {-0.5, 0.0, -1.0},
71+
{-0.5, 0.0, -0.5}, {-0.5, 0.0, 0.0}, {-0.5, 0.0, 0.5},
72+
{-0.5, 0.0, 1.0}, {-0.5, 0.5, -1.0}, {-0.5, 0.5, -0.5},
73+
{-0.5, 0.5, 0.0}, {-0.5, 0.5, 0.5}, {-0.5, 0.5, 1.0},
74+
{-0.5, 1.0, -1.0}, {-0.5, 1.0, -0.5}, {-0.5, 1.0, 0.0},
75+
{-0.5, 1.0, 0.5}, {-0.5, 1.0, 1.0}, {0.0, -1.0, -1.0},
76+
{0.0, -1.0, -0.5}, {0.0, -1.0, 0.0}, {0.0, -1.0, 0.5},
77+
{0.0, -1.0, 1.0}, {0.0, -0.5, -1.0}, {0.0, -0.5, -0.5},
78+
{0.0, -0.5, 0.0}, {0.0, -0.5, 0.5}, {0.0, -0.5, 1.0},
79+
{0.0, 0.0, -1.0}, {0.0, 0.0, -0.5}, {0.0, 0.0, 0.0},
80+
{0.0, 0.0, 0.5}, {0.0, 0.0, 1.0}, {0.0, 0.5, -1.0},
81+
{0.0, 0.5, -0.5}, {0.0, 0.5, 0.0}, {0.0, 0.5, 0.5},
82+
{0.0, 0.5, 1.0}, {0.0, 1.0, -1.0}, {0.0, 1.0, -0.5},
83+
{0.0, 1.0, 0.0}, {0.0, 1.0, 0.5}, {0.0, 1.0, 1.0},
84+
{0.5, -1.0, -1.0}, {0.5, -1.0, -0.5}, {0.5, -1.0, 0.0},
85+
{0.5, -1.0, 0.5}, {0.5, -1.0, 1.0}, {0.5, -0.5, -1.0},
86+
{0.5, -0.5, -0.5}, {0.5, -0.5, 0.0}, {0.5, -0.5, 0.5},
87+
{0.5, -0.5, 1.0}, {0.5, 0.0, -1.0}, {0.5, 0.0, -0.5},
88+
{0.5, 0.0, 0.0}, {0.5, 0.0, 0.5}, {0.5, 0.0, 1.0},
89+
{0.5, 0.5, -1.0}, {0.5, 0.5, -0.5}, {0.5, 0.5, 0.0},
90+
{0.5, 0.5, 0.5}, {0.5, 0.5, 1.0}, {0.5, 1.0, -1.0},
91+
{0.5, 1.0, -0.5}, {0.5, 1.0, 0.0}, {0.5, 1.0, 0.5},
92+
{0.5, 1.0, 1.0}, {1.0, -1.0, -1.0}, {1.0, -1.0, -0.5},
93+
{1.0, -1.0, 0.0}, {1.0, -1.0, 0.5}, {1.0, -1.0, 1.0},
94+
{1.0, -0.5, -1.0}, {1.0, -0.5, -0.5}, {1.0, -0.5, 0.0},
95+
{1.0, -0.5, 0.5}, {1.0, -0.5, 1.0}, {1.0, 0.0, -1.0},
96+
{1.0, 0.0, -0.5}, {1.0, 0.0, 0.0}, {1.0, 0.0, 0.5},
97+
{1.0, 0.0, 1.0}, {1.0, 0.5, -1.0}, {1.0, 0.5, -0.5},
98+
{1.0, 0.5, 0.0}, {1.0, 0.5, 0.5}, {1.0, 0.5, 1.0},
99+
{1.0, 1.0, -1.0}, {1.0, 1.0, -0.5}, {1.0, 1.0, 0.0},
100+
{1.0, 1.0, 0.5}, {1.0, 1.0, 1.0}};
101+
return p;
102+
}
103+

source/matplot/core/axes_type.cpp

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4573,8 +4573,20 @@ namespace matplot {
45734573
const std::vector<std::vector<double>> &y,
45744574
const std::vector<std::vector<double>> &u,
45754575
const std::vector<std::vector<double>> &v,
4576+
const std::vector<std::vector<double>> &m,
45764577
double scale, std::string_view line_spec) {
45774578
return this->quiver(flatten(x), flatten(y), flatten(u), flatten(v),
4579+
(m.empty()) ? std::vector<double>{} : flatten(m),
4580+
scale, line_spec);
4581+
}
4582+
4583+
/// Quiver - 2d x,y,u,v with no magnitude
4584+
vectors_handle axes_type::quiver(const std::vector<std::vector<double>> &x,
4585+
const std::vector<std::vector<double>> &y,
4586+
const std::vector<std::vector<double>> &u,
4587+
const std::vector<std::vector<double>> &v,
4588+
double scale, std::string_view line_spec) {
4589+
return this->quiver(x, y, u, v, std::vector<std::vector<double>>{},
45784590
scale, line_spec);
45794591
}
45804592

@@ -4652,6 +4664,22 @@ namespace matplot {
46524664
}
46534665

46544666
/// Quiver 3d - 2d vectors
4667+
vectors_handle axes_type::quiver3(const std::vector<std::vector<double>> &x,
4668+
const std::vector<std::vector<double>> &y,
4669+
const std::vector<std::vector<double>> &z,
4670+
const std::vector<std::vector<double>> &u,
4671+
const std::vector<std::vector<double>> &v,
4672+
const std::vector<std::vector<double>> &w,
4673+
const std::vector<std::vector<double>> &m,
4674+
double scale,
4675+
std::string_view line_spec) {
4676+
return this->quiver3(flatten(x), flatten(y), flatten(z), flatten(u),
4677+
flatten(v), flatten(w),
4678+
(m.empty()) ? std::vector<double>{} : flatten(m),
4679+
scale, line_spec);
4680+
}
4681+
4682+
/// Quiver 3d - 2d vectors with no magnitude
46554683
vectors_handle axes_type::quiver3(const std::vector<std::vector<double>> &x,
46564684
const std::vector<std::vector<double>> &y,
46574685
const std::vector<std::vector<double>> &z,
@@ -4669,16 +4697,28 @@ namespace matplot {
46694697
const std::vector<std::vector<double>> &u,
46704698
const std::vector<std::vector<double>> &v,
46714699
const std::vector<std::vector<double>> &w,
4700+
const std::vector<std::vector<double>> &m,
46724701
double scale,
46734702
std::string_view line_spec) {
4674-
auto [n, m] = size(z);
4675-
vector_1d x = iota(1., static_cast<double>(m));
4703+
auto [n, p] = size(z);
4704+
vector_1d x = iota(1., static_cast<double>(p));
46764705
vector_1d y = iota(1., static_cast<double>(n));
46774706
auto [xx, yy] = meshgrid(x, y);
4678-
return this->quiver3(xx, yy, z, u, v, w, scale, line_spec);
4707+
return this->quiver3(xx, yy, z, u, v, w, m, scale, line_spec);
4708+
}
4709+
4710+
/// Quiver 3d - Automatic x and y - 2d vectors no magnitude
4711+
vectors_handle axes_type::quiver3(const std::vector<std::vector<double>> &z,
4712+
const std::vector<std::vector<double>> &u,
4713+
const std::vector<std::vector<double>> &v,
4714+
const std::vector<std::vector<double>> &w,
4715+
double scale,
4716+
std::string_view line_spec) {
4717+
return this->quiver3(z, u, v, w, std::vector<std::vector<double>>{},
4718+
scale, line_spec);
46794719
}
46804720

4681-
/// Quiver 3d - magnitude included
4721+
/// Quiver 3d - no magnitude
46824722
vectors_handle axes_type::quiver3(
46834723
const std::vector<double> &x, const std::vector<double> &y,
46844724
const std::vector<double> &z, const std::vector<double> &u,

source/matplot/core/axes_type.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1413,6 +1413,15 @@ namespace matplot {
14131413
std::string_view line_spec = "");
14141414

14151415
/// Quiver - 2d x,y,u,v
1416+
vectors_handle quiver(const std::vector<std::vector<double>> &x,
1417+
const std::vector<std::vector<double>> &y,
1418+
const std::vector<std::vector<double>> &u,
1419+
const std::vector<std::vector<double>> &v,
1420+
const std::vector<std::vector<double>> &m,
1421+
double scale = 1.0,
1422+
std::string_view line_spec = "");
1423+
1424+
/// Quiver - 2d x,y,u,v with no magnitude
14161425
vectors_handle quiver(const std::vector<std::vector<double>> &x,
14171426
const std::vector<std::vector<double>> &y,
14181427
const std::vector<std::vector<double>> &u,
@@ -1436,6 +1445,17 @@ namespace matplot {
14361445
std::string_view line_spec = "");
14371446

14381447
/// Quiver 3d - 2d vectors
1448+
vectors_handle quiver3(const std::vector<std::vector<double>> &x,
1449+
const std::vector<std::vector<double>> &y,
1450+
const std::vector<std::vector<double>> &z,
1451+
const std::vector<std::vector<double>> &u,
1452+
const std::vector<std::vector<double>> &v,
1453+
const std::vector<std::vector<double>> &w,
1454+
const std::vector<std::vector<double>> &m,
1455+
double scale = 1.0,
1456+
std::string_view line_spec = "");
1457+
1458+
/// Quiver 3d - 2d vectors no magnitude
14391459
vectors_handle quiver3(const std::vector<std::vector<double>> &x,
14401460
const std::vector<std::vector<double>> &y,
14411461
const std::vector<std::vector<double>> &z,
@@ -1446,14 +1466,23 @@ namespace matplot {
14461466
std::string_view line_spec = "");
14471467

14481468
/// Quiver 3d - Automatic x and y - 2d vectors
1469+
vectors_handle quiver3(const std::vector<std::vector<double>> &z,
1470+
const std::vector<std::vector<double>> &u,
1471+
const std::vector<std::vector<double>> &v,
1472+
const std::vector<std::vector<double>> &w,
1473+
const std::vector<std::vector<double>> &m,
1474+
double scale = 1.0,
1475+
std::string_view line_spec = "");
1476+
1477+
/// Quiver 3d - Automatic x and y - 2d vectors no magnitude
14491478
vectors_handle quiver3(const std::vector<std::vector<double>> &z,
14501479
const std::vector<std::vector<double>> &u,
14511480
const std::vector<std::vector<double>> &v,
14521481
const std::vector<std::vector<double>> &w,
14531482
double scale = 1.0,
14541483
std::string_view line_spec = "");
14551484

1456-
/// Quiver 3d - no magnitude included
1485+
/// Quiver 3d - no magnitude
14571486
vectors_handle
14581487
quiver3(const std::vector<double> &x, const std::vector<double> &y,
14591488
const std::vector<double> &z, const std::vector<double> &u,

0 commit comments

Comments
 (0)