🛰️航天仿真算法库 SpaceAST 0.0.1
载入中...
搜索中...
未找到
Unit.hpp
浏览该文件的文档.
1
20
21#pragma once
22
23#include "AstGlobal.h"
24#include "Dimension.hpp"
25#include "AstUtil/Object.hpp"
26#include "AstUtil/SharedPtr.hpp"
27#include "AstUtil/StringView.hpp"
28#include "AstUtil/Constants.h"
29#include <cmath>
30#include <string>
31#include <vector>
32#include <memory>
33
34AST_NAMESPACE_BEGIN
35
36
44class Unit;
45class Quantity;
46
47
48
49namespace units
50{
51 extern AST_UTIL_API Unit none;
52
53 extern AST_UTIL_API Unit mm;
54 extern AST_UTIL_API Unit cm;
55 extern AST_UTIL_API Unit dm;
56 extern AST_UTIL_API Unit m;
57 extern AST_UTIL_API Unit km;
58
59 extern AST_UTIL_API Unit in;
60 extern AST_UTIL_API Unit ft;
61 extern AST_UTIL_API Unit yd;
62 extern AST_UTIL_API Unit mi;
63
64
65
66 extern AST_UTIL_API Unit sec;
67 extern AST_UTIL_API Unit s;
68 extern AST_UTIL_API Unit ms;
69 extern AST_UTIL_API Unit min;
70 extern AST_UTIL_API Unit hour;
71 extern AST_UTIL_API Unit h;
72 extern AST_UTIL_API Unit day;
73
74 extern AST_UTIL_API Unit kg;
75 extern AST_UTIL_API Unit g;
76 extern AST_UTIL_API Unit mg;
77 extern AST_UTIL_API Unit lb;
78
79 extern AST_UTIL_API Unit N;
80
81 extern AST_UTIL_API Unit deg;
82 extern AST_UTIL_API Unit rad;
83 extern AST_UTIL_API Unit arcsec;
84
85 extern AST_UTIL_API Unit A;
86
87 extern AST_UTIL_API Unit m2;
88
89 extern AST_UTIL_API Unit m3;
90 extern AST_UTIL_API Unit L;
91
92};
93
94
95
97AST_UTIL_API Unit aUnitMultiply(const Unit& unit1, const Unit& unit2);
98
100AST_UTIL_API Unit aUnitMultiply(const Unit& unit1, const Unit& unit2, StringView newname);
101
103AST_UTIL_API Unit aUnitDivide(const Unit& unit1, const Unit& unit2);
104
106AST_UTIL_API Unit aUnitDivide(const Unit& unit1, const Unit& unit2, StringView newname);
107
109AST_UTIL_API Unit aUnitPower(const Unit& unit, int exponent);
110
112AST_UTIL_API Unit aUnitPower(const Unit& unit, int exponent, StringView newname);
113
115AST_UTIL_API Unit aUnitInvert(const Unit& unit);
116
118AST_UTIL_API Unit aUnitInvert(const Unit& unit, StringView newname);
119
124AST_UTIL_API void aUnitFactorize(const Unit& unit, Unit& newUnit, double& scale);
125
129AST_UTIL_API void aUnitFactorize(Unit& unit, double& scale);
130
131
132
133
135class Unit
136{
137public:
138 friend Unit unit_multiply(const Unit& unit1, const Unit& unit2);
139 friend Unit unit_divide(const Unit& unit1, const Unit& unit2);
140 friend Unit unit_power(const Unit& unit, int exponent);
141 friend void unit_setname(Unit& unit, StringView name);
142
143 class UnitRep;
144 using UnitRepHandle = std::shared_ptr<UnitRep>;
145 using UnitRepPair = std::pair<UnitRepHandle, int>;
146 using SubUnitList = std::vector<UnitRepPair>;
147
148 using UnitRepHandleConst = std::shared_ptr<const UnitRep>;
149 using UnitRepPairConst = std::pair<UnitRepHandleConst, int>;
150 using SubUnitListConst = std::vector<UnitRepPairConst>;
151
154 class UnitRep{
155 public:
156 UnitRep(StringView name, double scale, Dimension dimension, const SubUnitListConst& subunits)
157 : name_{std::string(name)}
158 , scale_{scale}
159 , dimension_{dimension}
160 , subUnits_{subunits}
161 {}
162 std::string name_;
163 double scale_{1.0};
166 };
167public:
169 static Unit NaN()
170 {
171 return Unit("", 0.0, EDimension::eUnit);
172 }
173
175 static Unit None()
176 {
177 return Unit("", 1.0, EDimension::eUnit);
178 }
179
181 static Unit One()
182 {
183 // return Unit(aText("×1"), 1.0, EDimension::eUnit);
184 return Unit(aText("\u00D71"), 1.0, EDimension::eUnit);
185 }
186
190 AST_UTIL_API
191 static Unit Scale(double scale);
192
194 static Unit Percent()
195 {
196 return Unit("%", 0.01, EDimension::eUnit);
197 }
198
199
201 static Unit Second()
202 {
203 return Unit("s", 1.0, EDimension::eTime);
204 }
206 static Unit Minute()
207 {
208 return Unit("min", 60.0, EDimension::eTime);
209 }
211 static Unit Hour()
212 {
213 return Unit("h", 3600.0, EDimension::eTime);
214 }
216 static Unit Day()
217 {
218 return Unit("day", 86400.0, EDimension::eTime);
219 }
222 {
223 return Unit("ms", 0.001, EDimension::eTime);
224 }
225
226
227
229 static Unit Meter()
230 {
231 return Unit("m", 1.0, EDimension::eLength);
232 }
235 {
236 return Unit("km", 1000.0, EDimension::eLength);
237 }
240 {
241 return Unit("cm", 0.01, EDimension::eLength);
242 }
245 {
246 return Unit("dm", 0.1, EDimension::eLength);
247 }
248
251 {
252 return Unit("mm", 0.001, EDimension::eLength);
253 }
254
255
257 static Unit Inch()
258 {
259 return Unit("in", 0.0254, EDimension::eLength);
260 }
261
263 static Unit Foot()
264 {
265 return Unit("ft", 0.3048, EDimension::eLength);
266 }
267
269 static Unit Yard()
270 {
271 return Unit("yd", 0.9144, EDimension::eLength);
272 }
273
275 static Unit Mile()
276 {
277 return Unit("mi", 1609.344, EDimension::eLength);
278 }
279
281 static Unit Kilogram()
282 {
283 return Unit("kg", 1.0, EDimension::eMass);
284 }
286 static Unit Gram()
287 {
288 return Unit("g", 0.001, EDimension::eMass);
289 }
290
293 {
294 return Unit("mg", 0.000001, EDimension::eMass);
295 }
296
298 static Unit Pound()
299 {
300 return Unit("lb", 0.45359237, EDimension::eMass);
301 }
302
304 static Unit Radian()
305 {
306 return Unit("rad", 1.0, EDimension::eAngle);
307 }
308
310 static Unit Degree()
311 {
312 // return Unit("°", 0.017453292519943295, EDimension::eAngle);
313 return Unit(aText("\u00B0"), kDegToRad, EDimension::eAngle);
314 }
315
318 {
319 return Unit(aText("\u2033"), kArcSecToRad, EDimension::eAngle);
320 }
321
323 static Unit Newton()
324 {
325 return Unit("N", 1.0, EDimension::eForce);
326 }
327
329 static Unit Ampere()
330 {
331 return Unit("A", 1.0, EDimension::eCurrent);
332 }
334 static Unit Kelvin()
335 {
336 return Unit("K", 1.0, EDimension::eTemperature);
337 }
339 static Unit Mole()
340 {
341 return Unit("mol", 1.0, EDimension::eAmount);
342 }
343
344 // 下面是组合单位
345
348 return Centimeter() / Second();
349 }
352 {
353 return Kilometer() / Hour();
354 }
357 {
358 return Meter() / Minute();
359 }
362 {
363 return Meter() * Meter();
364 }
367 {
368 return Kilometer() * Kilometer();
369 }
372 {
373 return Foot() * Foot();
374 }
375
378 {
379 return Yard() * Yard();
380 }
381
384 {
385 return Meter() * Meter() * Meter();
386 }
388 static Unit Liter()
389 {
390 return Unit("L", 0.001, EDimension::eVolume);
391 }
392
393public:
395 Unit(): Unit{units::none}{}
396
398 Unit(StringView name, double scale, Dimension dimension)
399 : rep_{std::make_shared<UnitRep>(name, scale, dimension, SubUnitListConst{})}
400 {}
401
402protected:
403 Unit(double scale, Dimension dimension, const SubUnitListConst& subunits)
404 : rep_{std::make_shared<UnitRep>("", scale, dimension, (subunits))}
405 {}
406
408 Unit(UnitRepHandle rep) : rep_(rep) {}
409
410public:
414 double fromSI(double value) const { return value / getScale(); }
415
419 double toSI(double value) const { return value * getScale(); }
420
424 double convertTo(double value, const Unit& unit) const { return value * (this->getScale() / unit.getScale()); }
425
429 double convertFrom(double value, const Unit& unit) const { return value * (unit.getScale() / this->getScale()); }
430
433 double getScale() const { return rep_->scale_; }
434
437 Dimension dimension() const { return rep_->dimension_; }
438
441 const std::string& name() const { return rep_->name_; }
442
445 bool isValid() const { return rep_->scale_ != 0.0; }
446
447public: // operators
448
451 Unit clone() const { return Unit(std::make_shared<UnitRep>(*rep_)); }
452
454 Unit invert() const { return aUnitInvert(*this); }
455
460 Unit multiply(StringView newname, const Unit& other) const
461 {
462 return aUnitMultiply(*this, other, newname);
463 }
464
469 Unit divide(StringView newname, const Unit& other) const
470 {
471 return aUnitDivide(*this, other, newname);
472 }
473
478 Unit scale(StringView newname, double factor) const
479 {
480 return Unit(newname, factor * rep_->scale_, rep_->dimension_);
481 }
482
487 Unit pow(StringView newname, int exponent) const
488 {
489 return aUnitPower(*this, exponent, newname);
490 }
491
495 Unit pow(int exponent) const
496 {
497 return aUnitPower(*this, exponent);
498 }
499
503 bool operator==(const Unit& other) const
504 {
505 if(rep_ == other.rep_)
506 return true;
507 // 单位缩放因子和量纲相同即可认为是相同单位
508 return rep_->scale_ == other.rep_->scale_ && rep_->dimension_ == other.rep_->dimension_;
509 }
510
514 bool operator!=(const Unit& other) const
515 {
516 return !(*this == other);
517 }
518
522 Unit operator*(const Unit& other) const
523 {
524 return aUnitMultiply(*this, other);
525 }
526
530 Unit operator/(const Unit& other) const
531 {
532 return aUnitDivide(*this, other);
533 }
537 Unit& operator*=(const Unit& other)
538 {
539 *this = *this * other;
540 return *this;
541 }
545 Unit& operator/=(const Unit& other)
546 {
547 *this = *this / other;
548 return *this;
549 }
550
551
552public:
553 UnitRepHandle rep_;
554};
555
556
560AST_NAMESPACE_END
Unit m3
立方米
定义 Unit.cpp:447
Unit mg
毫克
定义 Unit.cpp:434
Unit s
定义 Unit.cpp:425
Unit A
安培
定义 Unit.cpp:443
Unit cm
厘米
定义 Unit.cpp:414
Unit kg
千克
定义 Unit.cpp:432
Unit h
小时
定义 Unit.cpp:429
Unit arcsec
弧秒
定义 Unit.cpp:441
Unit mi
英里
定义 Unit.cpp:422
Unit day
定义 Unit.cpp:430
Unit lb
定义 Unit.cpp:435
Unit dm
分米
定义 Unit.cpp:415
Unit hour
小时
定义 Unit.cpp:428
Unit ms
毫秒
定义 Unit.cpp:426
Unit L
定义 Unit.cpp:448
Unit yd
定义 Unit.cpp:420
Unit m
定义 Unit.cpp:416
Unit deg
定义 Unit.cpp:439
Unit mm
毫米
定义 Unit.cpp:413
Unit sec
定义 Unit.cpp:424
Unit ft
英尺
定义 Unit.cpp:421
Unit none
无单位
定义 Unit.cpp:411
Unit km
千米
定义 Unit.cpp:417
Unit rad
弧度
定义 Unit.cpp:440
Unit N
牛顿
定义 Unit.cpp:437
Unit m2
平方米
定义 Unit.cpp:445
Unit in
英寸
定义 Unit.cpp:419
Unit g
定义 Unit.cpp:433
量纲
定义 Dimension.hpp:354
单位表示
定义 Unit.hpp:154
SubUnitListConst subUnits_
子单位列表
定义 Unit.hpp:165
Dimension dimension_
量纲
定义 Unit.hpp:164
std::string name_
名称
定义 Unit.hpp:162
单位
定义 Unit.hpp:136
static Unit Newton()
牛顿单位
定义 Unit.hpp:323
std::vector< UnitRepPair > SubUnitList
单位表示子项列表
定义 Unit.hpp:146
Unit(StringView name, double scale, Dimension dimension)
新建单位
定义 Unit.hpp:398
static Unit Day()
天单位
定义 Unit.hpp:216
Unit multiply(StringView newname, const Unit &other) const
单位乘法
定义 Unit.hpp:460
std::vector< UnitRepPairConst > SubUnitListConst
单位表示子项列表(常量)
定义 Unit.hpp:150
static Unit Mole()
mole单位
定义 Unit.hpp:339
static Unit Meter()
米单位
定义 Unit.hpp:229
std::shared_ptr< UnitRep > UnitRepHandle
单位表示
定义 Unit.hpp:144
Unit scale(StringView newname, double factor) const
单位缩放
定义 Unit.hpp:478
double getScale() const
获取单位缩放因子
定义 Unit.hpp:433
static Unit Percent()
百分比单位
定义 Unit.hpp:194
static Unit Minute()
分钟单位
定义 Unit.hpp:206
bool operator!=(const Unit &other) const
单位不相等运算符
定义 Unit.hpp:514
Unit pow(StringView newname, int exponent) const
单位幂运算
定义 Unit.hpp:487
Unit divide(StringView newname, const Unit &other) const
单位除法
定义 Unit.hpp:469
Unit pow(int exponent) const
单位幂运算
定义 Unit.hpp:495
double toSI(double value) const
从该单位对应的值转换为国际单位制下的值
定义 Unit.hpp:419
Unit operator*(const Unit &other) const
单位乘法运算符
定义 Unit.hpp:522
static Unit Milligram()
毫克单位
定义 Unit.hpp:292
bool operator==(const Unit &other) const
单位相等运算符
定义 Unit.hpp:503
static Unit Second()
秒单位
定义 Unit.hpp:201
static Unit None()
无单位
定义 Unit.hpp:175
static Unit CubicMeter()
立方米单位
定义 Unit.hpp:383
static Unit NaN()
无效单位
定义 Unit.hpp:169
static Unit SquareMeter()
平方米单位
定义 Unit.hpp:361
static Unit Yard()
码单位
定义 Unit.hpp:269
Dimension dimension() const
获取单位量纲
定义 Unit.hpp:437
static Unit Gram()
克单位
定义 Unit.hpp:286
Unit & operator*=(const Unit &other)
单位乘法赋值运算符
定义 Unit.hpp:537
static Unit Kilometer()
米单位
定义 Unit.hpp:234
Unit invert() const
单位倒数
定义 Unit.hpp:454
static Unit SquareYard()
平方码单位
定义 Unit.hpp:377
std::shared_ptr< const UnitRep > UnitRepHandleConst
单位表示句柄(常量)
定义 Unit.hpp:148
static Unit Centimeter()
厘米单位
定义 Unit.hpp:239
static Unit Liter()
升单位
定义 Unit.hpp:388
const std::string & name() const
获取单位名称
定义 Unit.hpp:441
double convertFrom(double value, const Unit &unit) const
转换为指定单位下的值
定义 Unit.hpp:429
static Unit Ampere()
安培单位
定义 Unit.hpp:329
Unit clone() const
单位克隆
定义 Unit.hpp:451
static Unit Kelvin()
开尔文单位
定义 Unit.hpp:334
static Unit Foot()
英尺单位
定义 Unit.hpp:263
static Unit Hour()
小时单位
定义 Unit.hpp:211
static Unit SquareFoot()
平方英尺单位
定义 Unit.hpp:371
Unit & operator/=(const Unit &other)
单位除法赋值运算符
定义 Unit.hpp:545
bool isValid() const
是否有效单位
定义 Unit.hpp:445
static Unit CentimeterPerSecond()
厘米每秒单位
定义 Unit.hpp:347
static Unit Millimeter()
毫米单位
定义 Unit.hpp:250
double fromSI(double value) const
从国际单位制转换为该单位对应的值
定义 Unit.hpp:414
static Unit Decimeter()
分米单位
定义 Unit.hpp:244
std::pair< UnitRepHandleConst, int > UnitRepPairConst
单位表示子项(常量)
定义 Unit.hpp:149
double convertTo(double value, const Unit &unit) const
转换为指定单位下的值
定义 Unit.hpp:424
static Unit Radian()
弧度单位
定义 Unit.hpp:304
static Unit ArcSecond()
弧秒单位
定义 Unit.hpp:317
static Unit Mile()
英里单位
定义 Unit.hpp:275
Unit(UnitRepHandle rep)
定义 Unit.hpp:408
Unit()
默认无单位
定义 Unit.hpp:395
static Unit Inch()
英寸单位
定义 Unit.hpp:257
static Unit Degree()
度单位
定义 Unit.hpp:310
Unit operator/(const Unit &other) const
单位除法运算符
定义 Unit.hpp:530
static Unit One()
无量纲单位
定义 Unit.hpp:181
static Unit Pound()
磅单位
定义 Unit.hpp:298
static Unit MeterPerMinute()
米每分钟单位
定义 Unit.hpp:356
static Unit Kilogram()
千克单位
定义 Unit.hpp:281
static Unit MilliSecond()
毫秒单位
定义 Unit.hpp:221
static Unit KilometerPerHour()
千米每秒单位
定义 Unit.hpp:351
static Unit SquareKilometer()
平方公里单位
定义 Unit.hpp:366
std::pair< UnitRepHandle, int > UnitRepPair
单位表示子项
定义 Unit.hpp:145
Unit aUnitMultiply(const Unit &unit1, const Unit &unit2)
单位乘法
定义 Unit.cpp:329
Unit aUnitPower(const Unit &unit, int exponent)
单位幂
定义 Unit.cpp:357
Unit aUnitInvert(const Unit &unit)
单位倒数
定义 Unit.cpp:372
void aUnitFactorize(const Unit &unit, Unit &newUnit, double &scale)
单位分解
定义 Unit.cpp:386
Unit aUnitDivide(const Unit &unit1, const Unit &unit2)
单位除法
定义 Unit.cpp:343
constexpr double kArcSecToRad
弧秒到弧度 kDegToRad/3600.0 度分秒的秒
定义 Constants.h:213
constexpr double kDegToRad
角度到弧度 kPI/180.0
定义 Constants.h:212
Unit unit_divide(const Unit &unit1, const Unit &unit2)
单位除法
定义 Unit.cpp:263
Unit unit_power(const Unit &unit, int exponent)
单位幂
定义 Unit.cpp:296
void unit_setname(Unit &unit, StringView name)
设置单位名称
定义 Unit.cpp:323
Unit unit_multiply(const Unit &unit1, const Unit &unit2)
单位乘法
定义 Unit.cpp:232