View Javadoc
1   // ******************************************************************************
2   //
3   // Title:       Force Field X.
4   // Description: Force Field X - Software for Molecular Biophysics.
5   // Copyright:   Copyright (c) Michael J. Schnieders 2001-2024.
6   //
7   // This file is part of Force Field X.
8   //
9   // Force Field X is free software; you can redistribute it and/or modify it
10  // under the terms of the GNU General Public License version 3 as published by
11  // the Free Software Foundation.
12  //
13  // Force Field X is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16  // details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // Force Field X; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA 02111-1307 USA
21  //
22  // Linking this library statically or dynamically with other modules is making a
23  // combined work based on this library. Thus, the terms and conditions of the
24  // GNU General Public License cover the whole combination.
25  //
26  // As a special exception, the copyright holders of this library give you
27  // permission to link this library with independent modules to produce an
28  // executable, regardless of the license terms of these independent modules, and
29  // to copy and distribute the resulting executable under terms of your choice,
30  // provided that you also meet, for each linked independent module, the terms
31  // and conditions of the license of that module. An independent module is a
32  // module which is not derived from or based on this library. If you modify this
33  // library, you may extend this exception to your version of the library, but
34  // you are not obligated to do so. If you do not wish to do so, delete this
35  // exception statement from your version.
36  //
37  // ******************************************************************************
38  package ffx.openmm;
39  
40  import com.sun.jna.ptr.PointerByReference;
41  import edu.uiowa.jopenmm.OpenMM_Vec3;
42  
43  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_Vec3Array_append;
44  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_Vec3Array_create;
45  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_Vec3Array_destroy;
46  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_Vec3Array_get;
47  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_Vec3Array_getSize;
48  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_Vec3Array_resize;
49  import static edu.uiowa.jopenmm.OpenMMLibrary.OpenMM_Vec3Array_set;
50  
51  /**
52   * Vec3 Array.
53   */
54  public class Vec3Array {
55  
56    /**
57     * String vec3 array pointer.
58     */
59    private PointerByReference pointer;
60  
61    /**
62     * OpenMM Vec3 Array constructor.
63     *
64     * @param size The size of the String Array.
65     */
66    public Vec3Array(int size) {
67      pointer = OpenMM_Vec3Array_create(size);
68    }
69  
70    /**
71     * OpenMM Vec3 Array constructor.
72     *
73     * @param pointer The Vec3 Array pointer.
74     */
75    public Vec3Array(PointerByReference pointer) {
76      this.pointer = pointer;
77    }
78  
79    /**
80     * Get the pointer to the vec3 array pointer.
81     *
82     * @return The pointer to the vec3 array.
83     */
84    public PointerByReference getPointer() {
85      return pointer;
86    }
87  
88    /**
89     * Append a Vec3 to the Vec3Array.
90     *
91     * @param vec3 The Vec3 to append.
92     */
93    public void append(OpenMM_Vec3.ByValue vec3) {
94      OpenMM_Vec3Array_append(pointer, vec3);
95    }
96  
97    /**
98     * Get a Vec3 from the Vec3Array.
99     *
100    * @return The Vec3 at index i.
101    */
102   public OpenMM_Vec3 get(int i) {
103     return OpenMM_Vec3Array_get(pointer, i);
104   }
105 
106   /**
107    * Get the size of the Vec3Array.
108    *
109    * @return The size of the Vec3Array.
110    */
111   public int getSize() {
112     return OpenMM_Vec3Array_getSize(pointer);
113   }
114 
115   /**
116    * Resize the Vec3Array.
117    *
118    * @param size The new size of the Vec3Array.
119    */
120   public void resize(int size) {
121     OpenMM_Vec3Array_resize(pointer, size);
122   }
123 
124   /**
125    * Set a Vec3 in the Vec3Array.
126    *
127    * @param i    The index of the Vec3 to set.
128    * @param vec3 The Vec3 to set.
129    */
130   public void set(int i, OpenMM_Vec3.ByValue vec3) {
131     OpenMM_Vec3Array_set(pointer, i, vec3);
132   }
133 
134   /**
135    * Destroy the Vec3Array.
136    */
137   public void destroy() {
138     if (pointer != null) {
139       OpenMM_Vec3Array_destroy(pointer);
140       pointer = null;
141     }
142   }
143 
144   /**
145    * Convert a double array to a Vec3Array.
146    *
147    * @param array The double array.
148    * @return The Vec3Array.
149    */
150   public static Vec3Array toVec3Array(double[] array) {
151     Vec3Array vec3Array = new Vec3Array(0);
152     OpenMM_Vec3.ByValue vec3 = new OpenMM_Vec3.ByValue();
153     for (int i = 0; i < array.length; i += 3) {
154       vec3.x = array[i];
155       vec3.y = array[i + 1];
156       vec3.z = array[i + 2];
157       vec3Array.append(vec3);
158     }
159     return vec3Array;
160   }
161 
162   /**
163    * Convert the Vec3Array to a double array.
164    *
165    * @return The double array.
166    */
167   public double[] getArray() {
168     int size = getSize();
169     double[] array = new double[size * 3];
170     for (int i = 0; i < size; i++) {
171       OpenMM_Vec3 vec3 = get(i);
172       int index = i * 3;
173       array[index] = vec3.x;
174       array[index + 1] = vec3.y;
175       array[index + 2] = vec3.z;
176     }
177     return array;
178   }
179 
180 }