00001 00002 // This file is part of ff3d - http://www.freefem.org/ff3d 00003 // Copyright (C) 2005 Stephane Del Pino 00004 // 00005 // This program is free software; you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation; either version 2, or (at your option) 00008 // any later version. 00009 // 00010 // This program is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with this program; if not, write to the Free Software Foundation, 00017 // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00018 // 00019 // $Id: NormalManager.hpp,v 1.5 2007/08/02 23:11:54 delpinux Exp $ 00020 00021 #ifndef NORMAL_MANAGER_HPP 00022 #define NORMAL_MANAGER_HPP 00023 00024 #include <ThreadStaticBase.hpp> 00025 00026 #include <SurfElem.hpp> 00027 #include <ReferenceCounting.hpp> 00028 00041 class NormalManager 00042 : public ThreadStaticBase<NormalManager> 00043 { 00044 private: 00045 const SurfElem* __surfElem; 00047 size_t __counter; 00049 mutable ReferenceCounting<TinyVector<3, real_t> > 00050 __normal; 00056 void __buildNormal() const 00057 { 00058 if (__surfElem == 0) { 00059 throw ErrorHandler(__FILE__,__LINE__, 00060 "Cannot evaluate normal, no surface is defined", 00061 ErrorHandler::unexpected); 00062 } 00063 if (__normal == 0) { 00064 __normal = new TinyVector<3, real_t>(__surfElem->normal()); 00065 } 00066 } 00067 00068 public: 00074 void subscribe(const SurfElem* s) 00075 { 00076 __counter++; 00077 __surfElem = s; 00078 } 00079 00085 void update(const SurfElem* s) 00086 { 00087 ASSERT(__surfElem != 0); 00088 __surfElem = s; 00089 __normal = 0; // normal is to recompute 00090 } 00091 00096 void unsubscribe() 00097 { 00098 ASSERT(__counter != 0); 00099 __counter--; 00100 if (__counter == 0) { 00101 __surfElem = 0; 00102 __normal = 0; 00103 } 00104 } 00105 00111 real_t nx() const 00112 { 00113 __buildNormal(); 00114 return (*__normal)[0]; 00115 } 00116 00122 real_t ny() const 00123 { 00124 __buildNormal(); 00125 return (*__normal)[1]; 00126 } 00127 00133 real_t nz() const 00134 { 00135 __buildNormal(); 00136 return (*__normal)[2]; 00137 } 00138 00143 explicit NormalManager() 00144 : __surfElem(0), 00145 __counter(0), 00146 __normal(0) 00147 { 00148 ; 00149 } 00150 00155 ~NormalManager() 00156 { 00157 ASSERT(__counter == 0); 00158 ASSERT(__surfElem == 0); 00159 ASSERT(__normal == 0); 00160 } 00161 }; 00162 00163 #endif // NORMAL_MANAGER_HPP
1.5.6