🛰️航天仿真算法库 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 minute;
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 extern AST_UTIL_API Unit Pa;
93
94 extern AST_UTIL_API Unit K;
95};
96
97
98
100AST_UTIL_API Unit aUnitMultiply(const Unit& unit1, const Unit& unit2);
101
103AST_UTIL_API Unit aUnitMultiply(const Unit& unit1, const Unit& unit2, StringView newname);
104
106AST_UTIL_API Unit aUnitDivide(const Unit& unit1, const Unit& unit2);
107
109AST_UTIL_API Unit aUnitDivide(const Unit& unit1, const Unit& unit2, StringView newname);
110
112AST_UTIL_API Unit aUnitPower(const Unit& unit, int exponent);
113
115AST_UTIL_API Unit aUnitPower(const Unit& unit, int exponent, StringView newname);
116
118AST_UTIL_API Unit aUnitInvert(const Unit& unit);
119
121AST_UTIL_API Unit aUnitInvert(const Unit& unit, StringView newname);
122
127AST_UTIL_API void aUnitFactorize(const Unit& unit, Unit& newUnit, double& scale);
128
132AST_UTIL_API void aUnitFactorize(Unit& unit, double& scale);
133
134
135
136
138class Unit
139{
140public:
141 #ifdef AST_BUILD_LIB_UTIL
142 friend Unit unit_multiply(const Unit& unit1, const Unit& unit2);
143 friend Unit unit_divide(const Unit& unit1, const Unit& unit2);
144 friend Unit unit_power(const Unit& unit, int exponent);
145 friend void unit_setname(Unit& unit, StringView name);
146 #endif
147
148 class UnitRep;
149 using UnitRepHandle = std::shared_ptr<UnitRep>;
150 using UnitRepPair = std::pair<UnitRepHandle, int>;
151 using SubUnitList = std::vector<UnitRepPair>;
152
153 using UnitRepHandleConst = std::shared_ptr<const UnitRep>;
154 using UnitRepPairConst = std::pair<UnitRepHandleConst, int>;
155 using SubUnitListConst = std::vector<UnitRepPairConst>;
156
159 class UnitRep{
160 public:
161 UnitRep(StringView name, double scale, Dimension dimension, const SubUnitListConst& subunits)
162 : name_{std::string(name)}
163 , scale_{scale}
164 , dimension_{dimension}
165 , subUnits_{subunits}
166 {}
167 std::string name_;
168 double scale_{1.0};
171 };
172public:
174 static Unit NaN()
175 {
176 return Unit("", 0.0, EDimension::eUnit);
177 }
178
180 static Unit None()
181 {
182 return Unit("", 1.0, EDimension::eUnit);
183 }
184
186 static Unit One()
187 {
188 // return Unit(aText("×1"), 1.0, EDimension::eUnit);
189 return Unit(aText("\u00D71"), 1.0, EDimension::eUnit);
190 }
191
195 AST_UTIL_API
196 static Unit Scale(double scale);
197
199 static Unit Percent()
200 {
201 return Unit("%", 0.01, EDimension::eUnit);
202 }
203
204
206 static Unit Second()
207 {
208 return Unit("s", 1.0, EDimension::eTime);
209 }
211 static Unit Minute()
212 {
213 return Unit("min", 60.0, EDimension::eTime);
214 }
216 static Unit Hour()
217 {
218 return Unit("h", 3600.0, EDimension::eTime);
219 }
221 static Unit Day()
222 {
223 return Unit("day", 86400.0, EDimension::eTime);
224 }
227 {
228 return Unit("ms", 0.001, EDimension::eTime);
229 }
230
231
232
234 static Unit Meter()
235 {
236 return Unit("m", 1.0, EDimension::eLength);
237 }
240 {
241 return Unit("km", 1000.0, EDimension::eLength);
242 }
245 {
246 return Unit("cm", 0.01, EDimension::eLength);
247 }
250 {
251 return Unit("dm", 0.1, EDimension::eLength);
252 }
253
256 {
257 return Unit("mm", 0.001, EDimension::eLength);
258 }
259
260
262 static Unit Inch()
263 {
264 return Unit("in", 0.0254, EDimension::eLength);
265 }
266
268 static Unit Foot()
269 {
270 return Unit("ft", 0.3048, EDimension::eLength);
271 }
272
274 static Unit Yard()
275 {
276 return Unit("yd", 0.9144, EDimension::eLength);
277 }
278
280 static Unit Mile()
281 {
282 return Unit("mi", 1609.344, EDimension::eLength);
283 }
284
286 static Unit Kilogram()
287 {
288 return Unit("kg", 1.0, EDimension::eMass);
289 }
291 static Unit Gram()
292 {
293 return Unit("g", 0.001, EDimension::eMass);
294 }
295
298 {
299 return Unit("mg", 0.000001, EDimension::eMass);
300 }
301
303 static Unit Pound()
304 {
305 return Unit("lb", 0.45359237, EDimension::eMass);
306 }
307
309 static Unit Radian()
310 {
311 return Unit("rad", 1.0, EDimension::eAngle);
312 }
313
315 static Unit Degree()
316 {
317 // return Unit("°", 0.017453292519943295, EDimension::eAngle);
318 return Unit(aText("\u00B0"), kDegToRad, EDimension::eAngle);
319 }
320
323 {
324 return Unit(aText("\u2033"), kArcSecToRad, EDimension::eAngle);
325 }
326
328 static Unit Newton()
329 {
330 return Unit("N", 1.0, EDimension::eForce);
331 }
332
334 static Unit Ampere()
335 {
336 return Unit("A", 1.0, EDimension::eCurrent);
337 }
339 static Unit Kelvin()
340 {
341 return Unit("K", 1.0, EDimension::eTemperature);
342 }
343
345 // static Unit Mole()
346 // {
347 // return Unit("mol", 1.0, EDimension::eAmount);
348 // }
349
350 // 下面是组合单位
351
354 return Centimeter() / Second();
355 }
358 {
359 return Kilometer() / Hour();
360 }
363 {
364 return Meter() / Minute();
365 }
368 {
369 return Meter() * Meter();
370 }
373 {
374 return Kilometer() * Kilometer();
375 }
378 {
379 return Foot() * Foot();
380 }
381
384 {
385 return Yard() * Yard();
386 }
387
390 {
391 return Meter() * Meter() * Meter();
392 }
394 static Unit Liter()
395 {
396 return Unit("L", 0.001, EDimension::eVolume);
397 }
398
400 static Unit Pascal()
401 {
402 return Unit("Pa", 1.0, EDimension::ePressure);
403 }
404
405public:
407 Unit(): Unit{Unit::None()}{}
408
410 AST_UTIL_API
411 Unit(StringView name);
412
414 Unit(StringView name, double scale, Dimension dimension)
415 : rep_{std::make_shared<UnitRep>(name, scale, dimension, SubUnitListConst{})}
416 {}
417
418protected:
419 Unit(double scale, Dimension dimension, const SubUnitListConst& subunits)
420 : rep_{std::make_shared<UnitRep>("", scale, dimension, (subunits))}
421 {}
422
424 Unit(UnitRepHandle rep) : rep_(rep) {}
425
426public:
430 double fromSI(double value) const { return value / getScale(); }
431
435 double toSI(double value) const { return value * getScale(); }
436
440 double convertTo(double value, const Unit& unit) const { return value * (this->getScale() / unit.getScale()); }
441
445 double convertFrom(double value, const Unit& unit) const { return value * (unit.getScale() / this->getScale()); }
446
449 double getScale() const { return rep_->scale_; }
450
453 Dimension dimension() const { return rep_->dimension_; }
454
457 const std::string& name() const { return rep_->name_; }
458
461 bool isValid() const { return rep_->scale_ != 0.0; }
462
463public: // operators
464
467 Unit clone() const { return Unit(std::make_shared<UnitRep>(*rep_)); }
468
470 Unit invert() const { return aUnitInvert(*this); }
471
476 Unit multiply(StringView newname, const Unit& other) const
477 {
478 return aUnitMultiply(*this, other, newname);
479 }
480
485 Unit divide(StringView newname, const Unit& other) const
486 {
487 return aUnitDivide(*this, other, newname);
488 }
489
494 Unit scale(StringView newname, double factor) const
495 {
496 return Unit(newname, factor * rep_->scale_, rep_->dimension_);
497 }
498
503 Unit pow(StringView newname, int exponent) const
504 {
505 return aUnitPower(*this, exponent, newname);
506 }
507
511 Unit pow(int exponent) const
512 {
513 return aUnitPower(*this, exponent);
514 }
515
519 bool operator==(const Unit& other) const
520 {
521 if(rep_ == other.rep_)
522 return true;
523 // 单位缩放因子和量纲相同即可认为是相同单位
524 return rep_->scale_ == other.rep_->scale_ && rep_->dimension_ == other.rep_->dimension_;
525 }
526
530 bool operator!=(const Unit& other) const
531 {
532 return !(*this == other);
533 }
534
538 Unit operator*(const Unit& other) const
539 {
540 return aUnitMultiply(*this, other);
541 }
542
546 Unit operator/(const Unit& other) const
547 {
548 return aUnitDivide(*this, other);
549 }
553 Unit& operator*=(const Unit& other)
554 {
555 *this = *this * other;
556 return *this;
557 }
561 Unit& operator/=(const Unit& other)
562 {
563 *this = *this / other;
564 return *this;
565 }
566
567
568public:
569 UnitRepHandle rep_;
570};
571
572
576AST_NAMESPACE_END
Unit m3
立方米
定义 Unit.cpp:457
Unit mg
毫克
定义 Unit.cpp:444
Unit s
定义 Unit.cpp:435
Unit A
安培
定义 Unit.cpp:453
Unit minute
分钟,不要命名为min,容易与std::min冲突
定义 Unit.cpp:437
Unit cm
厘米
定义 Unit.cpp:424
Unit kg
千克
定义 Unit.cpp:442
Unit h
小时
定义 Unit.cpp:439
Unit arcsec
弧秒
定义 Unit.cpp:451
Unit mi
英里
定义 Unit.cpp:432
Unit day
定义 Unit.cpp:440
Unit lb
定义 Unit.cpp:445
Unit dm
分米
定义 Unit.cpp:425
Unit hour
小时
定义 Unit.cpp:438
Unit ms
毫秒
定义 Unit.cpp:436
Unit L
定义 Unit.cpp:458
Unit yd
定义 Unit.cpp:430
Unit m
定义 Unit.cpp:426
Unit deg
定义 Unit.cpp:449
Unit mm
毫米
定义 Unit.cpp:423
Unit sec
定义 Unit.cpp:434
Unit ft
英尺
定义 Unit.cpp:431
Unit none
无单位
定义 Unit.cpp:421
Unit Pa
帕斯卡
定义 Unit.cpp:460
Unit km
千米
定义 Unit.cpp:427
Unit rad
弧度
定义 Unit.cpp:450
Unit N
牛顿
定义 Unit.cpp:447
Unit m2
平方米
定义 Unit.cpp:455
Unit in
英寸
定义 Unit.cpp:429
Unit g
定义 Unit.cpp:443
Unit K
开氏温度
定义 Unit.cpp:462
量纲
定义 Dimension.hpp:360
单位表示
定义 Unit.hpp:159
SubUnitListConst subUnits_
子单位列表
定义 Unit.hpp:170
Dimension dimension_
量纲
定义 Unit.hpp:169
std::string name_
名称
定义 Unit.hpp:167
单位
定义 Unit.hpp:139
static Unit Newton()
牛顿单位
定义 Unit.hpp:328
std::vector< UnitRepPair > SubUnitList
单位表示子项列表
定义 Unit.hpp:151
Unit(StringView name, double scale, Dimension dimension)
新建单位
定义 Unit.hpp:414
static Unit Day()
天单位
定义 Unit.hpp:221
Unit multiply(StringView newname, const Unit &other) const
单位乘法
定义 Unit.hpp:476
std::vector< UnitRepPairConst > SubUnitListConst
单位表示子项列表(常量)
定义 Unit.hpp:155
static Unit Meter()
米单位
定义 Unit.hpp:234
std::shared_ptr< UnitRep > UnitRepHandle
单位表示
定义 Unit.hpp:149
Unit scale(StringView newname, double factor) const
单位缩放
定义 Unit.hpp:494
double getScale() const
获取单位缩放因子
定义 Unit.hpp:449
static Unit Percent()
百分比单位
定义 Unit.hpp:199
static Unit Minute()
分钟单位
定义 Unit.hpp:211
bool operator!=(const Unit &other) const
单位不相等运算符
定义 Unit.hpp:530
Unit pow(StringView newname, int exponent) const
单位幂运算
定义 Unit.hpp:503
Unit divide(StringView newname, const Unit &other) const
单位除法
定义 Unit.hpp:485
Unit pow(int exponent) const
单位幂运算
定义 Unit.hpp:511
double toSI(double value) const
从该单位对应的值转换为国际单位制下的值
定义 Unit.hpp:435
Unit operator*(const Unit &other) const
单位乘法运算符
定义 Unit.hpp:538
static Unit Pascal()
帕斯卡单位(压力量纲)
定义 Unit.hpp:400
static Unit Milligram()
毫克单位
定义 Unit.hpp:297
bool operator==(const Unit &other) const
单位相等运算符
定义 Unit.hpp:519
static Unit Second()
秒单位
定义 Unit.hpp:206
static Unit None()
无单位
定义 Unit.hpp:180
static Unit CubicMeter()
立方米单位
定义 Unit.hpp:389
static Unit NaN()
无效单位
定义 Unit.hpp:174
static Unit SquareMeter()
平方米单位
定义 Unit.hpp:367
static Unit Yard()
码单位
定义 Unit.hpp:274
Dimension dimension() const
获取单位量纲
定义 Unit.hpp:453
static Unit Gram()
克单位
定义 Unit.hpp:291
Unit & operator*=(const Unit &other)
单位乘法赋值运算符
定义 Unit.hpp:553
static Unit Kilometer()
米单位
定义 Unit.hpp:239
Unit invert() const
单位倒数
定义 Unit.hpp:470
static Unit SquareYard()
平方码单位
定义 Unit.hpp:383
std::shared_ptr< const UnitRep > UnitRepHandleConst
单位表示句柄(常量)
定义 Unit.hpp:153
static Unit Centimeter()
厘米单位
定义 Unit.hpp:244
static Unit Liter()
升单位
定义 Unit.hpp:394
const std::string & name() const
获取单位名称
定义 Unit.hpp:457
double convertFrom(double value, const Unit &unit) const
转换为指定单位下的值
定义 Unit.hpp:445
static Unit Ampere()
安培单位
定义 Unit.hpp:334
Unit clone() const
单位克隆
定义 Unit.hpp:467
static Unit Kelvin()
开尔文单位
定义 Unit.hpp:339
static Unit Foot()
英尺单位
定义 Unit.hpp:268
static Unit Hour()
小时单位
定义 Unit.hpp:216
static Unit SquareFoot()
平方英尺单位
定义 Unit.hpp:377
Unit & operator/=(const Unit &other)
单位除法赋值运算符
定义 Unit.hpp:561
bool isValid() const
是否有效单位
定义 Unit.hpp:461
static Unit CentimeterPerSecond()
mole单位
定义 Unit.hpp:353
static Unit Millimeter()
毫米单位
定义 Unit.hpp:255
double fromSI(double value) const
从国际单位制转换为该单位对应的值
定义 Unit.hpp:430
static Unit Decimeter()
分米单位
定义 Unit.hpp:249
std::pair< UnitRepHandleConst, int > UnitRepPairConst
单位表示子项(常量)
定义 Unit.hpp:154
double convertTo(double value, const Unit &unit) const
转换为指定单位下的值
定义 Unit.hpp:440
static Unit Radian()
弧度单位
定义 Unit.hpp:309
static Unit ArcSecond()
弧秒单位
定义 Unit.hpp:322
static Unit Mile()
英里单位
定义 Unit.hpp:280
Unit(UnitRepHandle rep)
定义 Unit.hpp:424
Unit()
默认无单位
定义 Unit.hpp:407
static Unit Inch()
英寸单位
定义 Unit.hpp:262
static Unit Degree()
度单位
定义 Unit.hpp:315
Unit operator/(const Unit &other) const
单位除法运算符
定义 Unit.hpp:546
static Unit One()
无量纲单位
定义 Unit.hpp:186
static Unit Pound()
磅单位
定义 Unit.hpp:303
static Unit MeterPerMinute()
米每分钟单位
定义 Unit.hpp:362
static Unit Kilogram()
千克单位
定义 Unit.hpp:286
static Unit MilliSecond()
毫秒单位
定义 Unit.hpp:226
static Unit KilometerPerHour()
千米每秒单位
定义 Unit.hpp:357
static Unit SquareKilometer()
平方公里单位
定义 Unit.hpp:372
std::pair< UnitRepHandle, int > UnitRepPair
单位表示子项
定义 Unit.hpp:150
Unit aUnitMultiply(const Unit &unit1, const Unit &unit2)
单位乘法
定义 Unit.cpp:330
Unit aUnitPower(const Unit &unit, int exponent)
单位幂
定义 Unit.cpp:358
Unit aUnitInvert(const Unit &unit)
单位倒数
定义 Unit.cpp:373
void aUnitFactorize(const Unit &unit, Unit &newUnit, double &scale)
单位分解
定义 Unit.cpp:387
Unit aUnitDivide(const Unit &unit1, const Unit &unit2)
单位除法
定义 Unit.cpp:344
constexpr double kArcSecToRad
弧秒到弧度 kDegToRad/3600.0 度分秒的秒
定义 Constants.h:229
constexpr double kDegToRad
角度到弧度 kPI/180.0
定义 Constants.h:228
Unit unit_divide(const Unit &unit1, const Unit &unit2)
单位除法
定义 Unit.cpp:264
Unit unit_power(const Unit &unit, int exponent)
单位幂
定义 Unit.cpp:297
void unit_setname(Unit &unit, StringView name)
设置单位名称
定义 Unit.cpp:324
Unit unit_multiply(const Unit &unit1, const Unit &unit2)
单位乘法
定义 Unit.cpp:233